StackOverflowException

greetings,

i keep getting a stackoverflowexception when i start this script (any help would be nice):

    using UnityEngine;
    using System.Collections;
    
    public class ExtraStats : MonoBehaviour {
    	public string leName;				//name of object giving stats
    
        public int baseStrength;
        public int baseWillpower;
        public int baseAttackSpeed;
        public int baseCritChance;
        public int baseCritDamage;
    
        public int baseDefence;
        public int baseMagicResist;
        public int baseHealth;
        public int baseAgility;
    
    	public float strength; 			    //physical damage
    	public float willpower; 		    //magical damage
        public float attackSpeed;           //time between attacks
    	public float critChance;		    //critical hit chance
    	public float critDamage;		    //how much times the damage of a critical hit is
    
    	public float defence; 			    //physical resistance
    	public float magicResist; 		    //magical resistance
    	public float health; 			    //hit points
        public float agility;               //dodge chance %
    
        public float baseUpgradeCost;        //base amount of upgrades
    	public float upgradeCost;		    //amount to upgrade
    	public float upgradeCostIncrease;   //% of cost increase
    	public int level; 				    //exp level
    
        private bool start = true;
    
        void Awake() {
            for (int i = 0; i < level; i++) {
                UpdateStats();
            }
        }
    
        public void SetLevel(int Level) {
            level = Level;
            upgradeCost = baseUpgradeCost;
            strength = baseStrength;
            willpower = baseWillpower;
            attackSpeed = baseAttackSpeed;
            critChance = baseCritChance;
            critDamage = baseCritDamage;
            defence = baseDefence;
            magicResist = baseMagicResist;
            health = baseHealth;
            agility = baseAgility;
            if (level > 1) {
                for (int i = 0; i <= level; i++) {
                    UpdateStats();
                }
            }
        }
        public void LevelUp() {
            level++;
            UpdateStats();
        }
        void UpdateStats() {
            upgradeCost *= 1.15f;
            strength *= 1.15f;
            willpower *= 1.15f;
            attackSpeed *= 1.15f;
            critChance *= level;
            critDamage *= 1.15f;
            defence *= 1.15f;
            magicResist *= 1.15f;
            health *= 1.15f;
            agility *= level;
            if (start) {
                gameObject.SendMessage("UpdateStats");
            }
            else {
                start = false;
                gameObject.SendMessage("StartExtraStats");
            }
        }
    }

The reason might be in line 76, where you call UpdateStats inside UpdateStats, thereby creating an the StackOverflow.

Stack Overflow means the same bit of code gets repeated infinitely, not allowing your game to continue with its code flow. Your for loops are the culprit here - most likely the variable Level is getting increased at the same rate as/faster than the variable i.

I’m guessing another script is calling the method LevelUp based on the updates getting done in the for loop. Whatever function is receiving StartExtraStats, I’d make sure it can’t call LevelUp, or any function that affects the level.

For future ref, figuring out where a StackOverflow begins and what’s causing it is pretty easy to do, just fill your code with Debug.Log()s to track where it manages to get before it crashes! :slight_smile: