The requested operation caused a stack overflow. Help

So I made this test GUI and attached it to the main camera, when I pressed play Unity crashed and noticed the error
“StackOverflowException: The requested operation caused a stack overflow.
BaseCharacterClass.set_HealthPoints (Int32 value) (at Assets/MyScripts/Character Classes/BaseCharacterClass.cs:24)”

using UnityEngine;
using System.Collections;

public class BaseCharacterClass {

    private string characterClassName;
    private string characterClassDescription;
    //Stats
    private int healthPoints;
    private int defense;
    private int strength;
    private int intellect;

    public string CharacterClassName {
        get {return characterClassName;}
        set {characterClassName = value;}
    }
    public string CharacterClassDescription {
        get {return characterClassDescription;}
        set {characterClassDescription = value;}
    }
    public int HealthPoints {
        get {return HealthPoints;}
        set {HealthPoints = value;}
    }
    public int Defense {
        get {return Defense;}
        set {Defense = value;}
    }

    public int Strength {
        get {return Strength;}
        set {Strength = value;}
    }
    public int Intellect {
        get {return Intellect;}
        set {Intellect = value;}
    }
}

It doesn’t only happen with HealthPoints, but also with Defense, Strength and Intellect. You have a recursion in all of them, because they call themselves. In the get and set, you need to make sure to use the lower case variables!
What happens at the moment is set the health points through the HealthPoints property. You assign the value to the HealthPoints property, which gets called again, again, again, …

10 Likes

Oh I get it, Thank you so much I’m still kinda new to programming so I’m really having a hard time when I get an error.
Thanks again