Stack Overflow Exception Unity C# Scripting
so i am making my first game in unity and i hit my first wall. i am watching some guides to make the game and i ended up with stack overflow exception. any help is really appreciated. i am using unity 5.5.0f3 x64 bit Personal version (free version), with microsoft visual studio that came along with the unity installation. thank you very much and sorry for long post.
This is the error StackOverflowException: The requested operation caused a stack overflow. BaseCharacterClass.set_CharacterClassName (System.String value) (at Assets/Scripts/Character Classes/BaseCharacterClass.cs:11).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseCharacterClass
{
public string CharacterClassName
{
get { return CharacterClassName; }
set { CharacterClassName = value; }
}
public string CharacterClassDescription
{
get { return CharacterClassDescription; }
set { CharacterClassDescription = value; }
}
public int Health
{
get { return Health; }
set { Health = value; }
}
public int Mana
{
get { return Mana; }
set { Mana = value; }
}
public int Strength
{
get { return Strength; }
set { Strength = value; }
}
public int Speed
{
get { return Speed; }
set { Speed = value; }
}
public int Intelligence
{
get { return Intelligence; }
set { Intelligence = value; }
}
public float AttackChance
{
get { return AttackChance; }
set { AttackChance = value; }
}
public float Defense
{
get { return Defense; }
set { Defense = value; }
}
public float Evasion
{
get { return Evasion; }
set { Evasion = value; }
}
public float Resistance
{
get { return Resistance; }
set { Resistance = value; }
}
}
warrior class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseWarriorClass : BaseCharacterClass
{
public BaseWarriorClass()
{
CharacterClassName = "Warrior";
CharacterClassDescription = "Melee Fighter, Later Can Evolve Into Breserker, Barbarian Or Knight";
Health = 10;
Mana = 0;
Strength = 5;
Speed = 5;
Intelligence = 0;
AttackChance = 50;
Defense = 5;
Evasion = 0;
Resistance = 0;
}
}
GUI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestGUI : MonoBehaviour
{
private BaseCharacterClass class1 = new BaseWarriorClass();
private BaseCharacterClass class2 = new BaseMageClass();
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
GUILayout.Label(class1.CharacterClassName);
GUILayout.Label(class1.CharacterClassDescription);
GUILayout.Label(class2.CharacterClassName);
GUILayout.Label(class2.CharacterClassDescription);
}
}
Answer by KoenigX3 · Jan 16, 2017 at 12:29 PM
You don't need these set / get methods in the BaseCharacterClass. If those variables are public, they can be modified without the methods. Right now they don't have any meaning.
You could use them for example, if you have a private variable, and you want to access it by a public set / get method.
Your answer
Follow this Question
Related Questions
[Solved] Recursive Method causing Stack Overflow 1 Answer
Unity freezing?? 2 Answers
problem with stackoverflow exception 2 Answers
Stack Overflow error in recursive method 0 Answers