Search Unity

c# getters and setters causing stack overflow

Discussion in 'Scripting' started by steveh2112, Jan 24, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    235
    i'm trying to make my code a bit more OO and so i added getter and setters on a few publics to limit access like this
    Code (CSharp):
    1.     public GameObject _HitTargetGO
    2.     {
    3.         get { return _HitTargetGO; }
    4.         private set { _HitTargetGO = value; }
    5.     }
    Unfortunately, it sends unity mad, lots of stack overflow exemptions then crashes.
    any idea what i'm doing wrong?
    thanks
    PS, public GameObject _HitTargetGO; without the get and set works fine
     
    Last edited: Jan 24, 2020
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,058
    You probably want to look into properties. Right now, you're doing a return, but you're telling it to return the property, which calls the get again, which tells it to return a property, which calls the get again...see the problem?

    Usually when you do a get and set, you target a different variable. Or, you just don't have a target return or set.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties

    The name property example is probably the closest to what you are trying to achieve.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,972
    infinite loop!!!!!!
     
    Joe-Censored likes this.
  4. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    290
    You're also calling private set on a public value. Just do:
    Code (CSharp):
    1. public GameObject _HitTargetGO
    2. {
    3.     get; private set;
    4. }
     
    steveh2112 likes this.
  5. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    235
    hum, i got the basic code from a c# tutorial so surprised its broken, but yes, i see why would get an infinite loop with this.
    i don't like the idea or creating 2 separate things to represent one thing however.
    thanks anyhow
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,058
    The purpose is to lock it behind code. You can do as suggested, just a get and set. However, normally you might have a field variable that you want other code to have access to, but you want to do some checks before you change it's value. For example, let's say you have a money variable. Then someone makes a withdraw, but you need to make sure they have enough money before you let them make the withdraw (very basic example). You could do a check to make sure they have enough instead of just changing the value directly.

    Properties serve their purposes. Not to say you need this, just that they have a reason for setting things up.
     
  7. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,020
    grizzly showed you how to get that same functionality without explicitly declaring a backing variable. C# will create the backing variable for you, and it will be invisible to you... specifically because people would rather not have "2 separate things to represent one thing" if they can help it.
     
    steveh2112 likes this.
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    936
    If like like puzzles, great, but ... getters and setters are a C# thing, not an OOP thing. OOP was fully created without them. In fact, we thought about properties way back, but decided they did more harm than good.

    Basic OOP is simply private variables and functions. For example:
    Code (CSharp):
    1. private AIvars myTagetting; // has the target, plus other stuff
    2.  
    3. public Transform currentTarget() { // like a "get"
    4.   if(AIvars.atttackTarget!=null) return AIvars.attackTarget;
    5.   return AIvars.assistTarget;
    6. }
    7.  
    8. public setTarget(Transform fightMe) { // like a "set"
    9.   if(AIvars.attackTarget!=fightMe) // is this a new attack target?
    10.     AIvars.acquireTime=Time.time; // record-keeping for "time on target"
    11.   AIvars.attackTarget=fightMe; // may become an assist target, later on
    12. }
    The user has always seen currentTarget() and setTarget(Transform). Originally myTarget was just a simple private var, but later on we added the AIvars struct with extra targeting info, updating the 2 functions to work with it. That's what OOP is about: here are the functions: I may change the interior to work better, which you won't see and don't want to see; but I will never change how you use them.
     
  9. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    235
    cool, i didn't see his post when i replied but that looks perfectly simple

    just tried it and works fine, very very useful.
     
    Last edited: Jan 25, 2020
unityunity