Search Unity

StackOverflowException: The requested operation caused a stack overflow.

Discussion in '2D' started by sportifsisman, Jul 31, 2020.

  1. sportifsisman

    sportifsisman

    Joined:
    Jul 17, 2020
    Posts:
    20
    public static Player Ornek
    {
    get
    {
    if (Player.Ornek == null)
    {
    return Ornek;
    }

    else
    {
    Ornek = GameObject.FindObjectOfType<Player>();
    }

    return Ornek;
    }
    set
    {
    Ornek = value;
    }
    }

    Hey Guys. I am trying to programming a platform game. Codes which was located top give me that error "StackOverflowException: The requested operation caused a stack overflow." when ı push the keyboard to get jump the character. could you help me pls.
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    Code (CSharp):
    1.  
    2. public static Player Ornek
    3. {
    4.    get
    5.     {
    6.          if (Player.Ornek == null)
    7.          {
    8.              return Ornek;
    9.          }
    10.          else
    11.          {
    12.              Ornek = GameObject.FindObjectOfType<Player>();
    13.          }
    14.     }
    15. }
    (btw, code blocks make code much easier to read for the people helping you).

    Look at line #6 above. You are accessing the very same getter you're inside of! Player.Ornek is is going to go into the getter again, which is going to find Player.Ornek, and go into the getter again, and so on. What you probably want is a private backing field:

    Code (CSharp):
    1. private static Player _ornek;
    2. public static Player Ornek
    3. {
    4.    get
    5.     {
    6.          if (Player._ornek != null)
    7.          {
    8.              return _ornek;
    9.          }
    10.          else
    11.          {
    12.              _ornek = GameObject.FindObjectOfType<Player>();
    13.              return _ornek;
    14.          }
    15.     }
    16. }
    And so on, the same applying to your setter as well.
    Note: in the above, I also changed == null to != null because I suspect that's what you intend to be doing: if there's no ornek, find it and return it.
     
    Derekloffin likes this.
  3. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    This code is an infinite recursion, thus the stack overflow.
    To explain, let's say something asks for the current value of Ornek, this is what it does:
    Get > if (player.ornek...
    Okay, that needs to evaluate the get again, so...
    Get > if (player.ornek...
    Okay, that needs to evaluate the get again, so...
    Get > if (player.ornek...
    Okay, that needs to evaluate the get again, so...
    and on and on and on it goes forever till the stack overflows.

    You probably want another private variable like _ornek and use that to do this check instead,
    something like this:
    Code (CSharp):
    1. private static Player _Ornek
    2.  
    3. public static Player Ornek
    4. {
    5.     get
    6.     {
    7.         if (Player._Ornek != null)
    8.         {
    9.             return Player._Ornek;
    10.         }
    11.         else
    12.         {
    13.             Player._Ornek = GameObject.FindObjectOfType<Player>();
    14.          }
    15.          return Player._Ornek;
    16.     }
    17.     set
    18.     {
    19.         _Ornek = value;
    20.      }
    21. }
     
  4. sportifsisman

    sportifsisman

    Joined:
    Jul 17, 2020
    Posts:
    20
    thank you so much for you support but ı've got one more failure. the character jump just one time. do you have any solution for that. thank you ahead.