Integer variable appears to lose its value

Hi all, I’ve run into something bizar and was hoping someone could take a look to see if I’m missing something obvious.

There are a couple of pieces of information that have led me to the conclusion posted in the title.
First of all, I have no error messages. As far as I can tell, there is nothing erroneous about the code.
Second, when using Visual Studio to debug with breakpoints, everything runs as expected, there aren’t any lines of code being skipped when they shouldn’t be.
Thirdly, and this is what led me to make a post here, when I have the setter property of the relevant variable print the new value to the console every time it changes, it never goes above 1 (it should), but also never goes back to 0 (default).

So, hopefully that gave a reasonable image of the issue I’m having, if not please feel free to ask for more info.
Now on to the code that I’m using, first of all, the get/set properties:

    private int _currWave;
    private int currWave
    {
        get => _currWave;
        set {
            _currWave = value;
            Debug.Log("currWave = " + value);
        }
    }

Next, there are only two places the variable value could possibly be modified (in the same class).
When going to a new wave of enemies:

    public void nextWave()
    {
        if (!this.hasEnemies)
            return;

        try
        {
            waves[currWave].Activate();
        }
        catch (IndexOutOfRangeException)
        {
            if (!infiniteWaves)
                Finished();
            else
                Reset();
        } finally
        {
            currWave++;
        }
    }

When the room is enabled or reset:

    private void OnEnable()
    {
        Reset();
    }

    public void Reset()
    {
        if (!hasEnemies)
            return;

        currWave = 0;
        finished = false;

        foreach (Wave wave in waves)
            wave.Reset();

        nextWave();
    }

Here is a screenshot of the console outputs I’m getting:

The only conclusion I can think of right now is that the variable somehow returns to a value of 0, because it never goes above 1. But at the same time, the setter property should be logging that to the console if/when it happens, but it never does…

I am at a complete loss, if you have any idea how this could be happening please let me know.
All replies are appreciated!

I would recommend a setter like this:

set {
    Debug.Log("currWave::setter old value: " + _currWave + " new value: " + value, gameObject);
    _currWave = value;
}

Note that important second parameter. When you see those log statements, just click on the message in the console. Unity will now highlight this context object in the hierarchy / project so you can see where it came from. I guess you may have this script on more than one object and you’re calling the method on the wrong object. Note that prefabs themselfs are also objects, they just don’t live active in the scene but you can still call methods or use / modify variables on them.

Another thing you want to check is that nobody is actually accessing “_currWave” besides your property getter / setter.

Well, it turned out that I had dragged the wrong room number into my enemies, so they were accessing the currWave variable of the wrong room.
Thanks for your help, I would have been stuck on this for many more hours if it wasn’t for your idea to log the gameObject!

1 Like

Yep, it’s one of those really useful hidden features you should know about :slight_smile: