c# getters and setters causing stack overflow

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

    public GameObject _HitTargetGO
    {
        get { return _HitTargetGO; }
        private set { _HitTargetGO = value; }
    }

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

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.

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

3 Likes

infinite loop!!!

1 Like

You’re also calling private set on a public value. Just do:

public GameObject _HitTargetGO
{
    get; private set;
}
1 Like

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

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.

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.

1 Like

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:

private AIvars myTagetting; // has the target, plus other stuff

public Transform currentTarget() { // like a "get"
  if(AIvars.atttackTarget!=null) return AIvars.attackTarget;
  return AIvars.assistTarget;
}

public setTarget(Transform fightMe) { // like a "set"
  if(AIvars.attackTarget!=fightMe) // is this a new attack target?
    AIvars.acquireTime=Time.time; // record-keeping for "time on target"
  AIvars.attackTarget=fightMe; // may become an assist target, later on
}

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.

cool, i didn’t see his post when i replied but that looks perfectly simple

just tried it and works fine, very very useful.