I'm getting a StackOverFlowException

So from what I’ve seen a SOE is linked to things that recur, like referencing a method: MethodA from within MethodA.

I haven’t seen anything like that in my script and would like some help. I also don’t have any errors from building the script.

It should be noted that the error occurs when my units are close to the enemy. However since that relates to most of the script, it’s not a lot to go on.

So the error:

UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransformBindings.gen.cs: 26)

It then lists lines in my script:

HopliteCombat.CheckEnemies () (at Assets/Scripts/Gameplay/HopliteCombat.cs: 67)

Which is this line:

 Collider[] enemyColliders = Physics.OverlapSphere(gameObject.transform.position, combatRadius);

And line 109:

if (CheckEnemies() == "Hoplite")

And there are a bunch of other references.

Here is the whole script, since multiple methods are referenced I don’t think it would be wise to show only snippets:

using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class HopliteCombat : MonoBehaviour {

    //EXT SCRIPTS
    public Movement move;
    private Hoplite hoplite;

    //HEALTH
    private HealthBar health;   
    public GameObject canvas;
    private bool healthBarUp = false;
    //STATS
    public float attackSpeed = 2.0f; 
    public float combatRadius = 20.0f;

    //UNITS
 
    public GameObject enemy;
    public NavMeshAgent leaderAgent;

 
    void Update()
    {

        if (CheckDistance() < 4.0f && move.enemySelected == true)
        {
            canvas.SetActive(true);
            if (healthBarUp)
            {
                canvas.transform.position += new Vector3(0, 2, 0);
                healthBarUp = true;
            }


            Debug.Log("engaging is true");
           
            //stop moving
            leaderAgent.Stop();

            Attack();
        }
        else {canvas.SetActive(false);}
        
    }


    private float CheckDistance()
    {
        float distance = Vector3.Distance(transform.position, enemy.transform.position);

        return distance;

    }

    private string CheckEnemies()
    {
        //create a sphere that detects units in a 20.0f radius. Loop through and find the closest collider, return the tag of the go.

        float bestDistance = 9000.0f;
        Collider bestCollider = null;

        Collider[] enemyColliders = Physics.OverlapSphere(gameObject.transform.position, combatRadius);

        foreach (Collider opponent in enemyColliders)
        {
            float distance = Vector3.Distance(gameObject.transform.position, opponent.transform.position);

            if (distance < bestDistance)
            {
                bestDistance = distance;
                bestCollider = opponent;
            }


        }
        return bestCollider.gameObject.tag;
    }

    private GameObject ReturnLeader()
    {

        Collider[] enemyColliders = Physics.OverlapSphere(gameObject.transform.position, combatRadius);

        foreach (Collider opponent in enemyColliders)
        {
            if (opponent.tag == "Leader")
            {
                return opponent.gameObject;
            }

            
        }
        return null;

    }


    private float Attack()
    {

        float finalAttackDamage = default(float);
        
        //Check tag of nearest enemy, factor in counter damage(s).
        if (CheckEnemies() == "Hoplite")
        {
            hoplite = GetComponent<Hoplite>();

            finalAttackDamage = hoplite.baseDamage;

        }

        if (CheckEnemies() == "Archer")
        {
            hoplite = GetComponent<Hoplite>();

            finalAttackDamage = hoplite.baseDamage + hoplite.counterArcher;
            return finalAttackDamage;
        }

        if (CheckEnemies() == "Peltast")
        {
            hoplite = GetComponent<Hoplite>();

            finalAttackDamage = hoplite.baseDamage + hoplite.counterPeltast;
            return finalAttackDamage;
        }

        if (CheckEnemies() == "Slinger")
        {
            hoplite = GetComponent<Hoplite>();

            finalAttackDamage = hoplite.baseDamage + hoplite.counterSlinger;
            return finalAttackDamage;
        }

        if (CheckEnemies() == "Cavalry")
        {
            hoplite = GetComponent<Hoplite>();

            finalAttackDamage = hoplite.baseDamage;
            return finalAttackDamage;
        }
        //After two seconds apply damage.
        attackSpeed -= Time.deltaTime;

        if (attackSpeed <= 0)
        {
            GiveDamage();

        }

        return 0.0f;

     }

    public void GiveDamage()
    {
        health = ReturnLeader().GetComponent<HealthBar>();

        health.TakeDamage(Attack());
    }

}

Attack method calls GiveDamage in line 153 and GiveDamage calls the Attack method in line 165. So you have a recursion that’s bouncing between the two methods until you run out of stack.