Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

The requested operation caused a stack overflow. Help

Discussion in 'Editor & General Support' started by CaptainBagel, Jan 2, 2015.

  1. CaptainBagel

    CaptainBagel

    Joined:
    Dec 7, 2014
    Posts:
    6
    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)"

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BaseCharacterClass {
    5.  
    6.     private string characterClassName;
    7.     private string characterClassDescription;
    8.     //Stats
    9.     private int healthPoints;
    10.     private int defense;
    11.     private int strength;
    12.     private int intellect;
    13.  
    14.     public string CharacterClassName {
    15.         get {return characterClassName;}
    16.         set {characterClassName = value;}
    17.     }
    18.     public string CharacterClassDescription {
    19.         get {return characterClassDescription;}
    20.         set {characterClassDescription = value;}
    21.     }
    22.     public int HealthPoints {
    23.         get {return HealthPoints;}
    24.         set {HealthPoints = value;}
    25.     }
    26.     public int Defense {
    27.         get {return Defense;}
    28.         set {Defense = value;}
    29.     }
    30.  
    31.     public int Strength {
    32.         get {return Strength;}
    33.         set {Strength = value;}
    34.     }
    35.     public int Intellect {
    36.         get {return Intellect;}
    37.         set {Intellect = value;}
    38.     }
    39. }
    40.  
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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, ...
     
    Sidy86, vadimca, OldOrion and 5 others like this.
  3. CaptainBagel

    CaptainBagel

    Joined:
    Dec 7, 2014
    Posts:
    6
    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