0

I would like to change a simple value in an array at a specific position, but even if the value within the array is not 0, when trying to change the value, this becomes 0. This happens in a simple loop like:

for (int i = 0; i < 4; i++)
{
    int stat = child[i];   // stat becomes 0 even if child[i] is != 0
    // some calculation on stat here
}

I've used VS17 debugger to try to understand what it is exactly happening, but I still don't know why this happens. In my application I've already set other variables to elements in array, but this is the first time I've seen anything like that. According to the debugger, stat = 0, even if child[i] != 0:

enter image description here

This happens thoughout all the loop. I'm sure that it is something that I'm missing, but I really can't get what it is.

3
  • 7
    you need to execute the line of code. You stopped right before it updated the value of stat you need to go to the next line to see the value afterwards. Commented Feb 8, 2018 at 19:05
  • Read my reply to the other answer, please Commented Feb 8, 2018 at 19:10
  • 1
    Add Debug.Log(string.Format("stat is {0}", stat)); I bet it's not zero. When the variable assignment is the only statement in the loop it's hard to say what the scope is. At what point in the loop is stat in scope and has its value? There is no statement that exists in this state. By adding a Debog.Log statement, you introduce a statement that does have these properties (stat is in scope post assignment) Commented Feb 8, 2018 at 19:26

1 Answer 1

6

While the debugger is stopped on the line like that, it hasn't executed that line of code yet. Since the line hasn't been executed, stat hasn't been assigned the value of child[i] and the debugger just shows the default value for an integer (0). It's working just fine. If you want to see the value change, go to the next statement.

Sign up to request clarification or add additional context in comments.

3 Comments

I know that, but even after the execution of the code "stat" is still 0. I know this because the returned child has 0 at indexes 0-3. Oherwise I would't had any problems...
Remember that each time you go around the loop, the variable stat get's reinitialized again. So if you don't stop and check the value after that line but before the loop goes back again, you won't see the change. From where your debugger has stopped, hit F10 and it should move to the close brace. Then check the value of stat. It should be what you are expecting.
@Koosshh56 You should probably change the screenshot you provided then, because it's the most obvious problem as is -- that you haven't yet executed the line of code. Also, that the code you depict is not "changing" child it is only "reading" from it. Furthermore, you haven't shown how you observe that the values of the array that you pass in do not get modified. (e.g. was a copy of the array passed to checkValidityStats?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.