[Solved] goes to a guess of 0 and stays when i press up or down

when i start the game it says what it is supposed to then when i press up or down it goes to 0 and stays.


Observed
It has a guess of higher or lower than 0 every time i press up or down.
Expect
For it to guess normally when i press up and down
Steps to reproduce
Figure out where it’s happening.
Change the code to what it should be.
Make sure it works.
Useful information
Code: https://gist.github.com/YourEngineer/7bbf098b16a1ac4a09a4b2602631037a
tried:
setting the variables to their numbers before game begins
getting rid of max = max + 1

Seems that the if statement there is a little off since there is no if (), just else if(), you should always start with if and then you can use else if or else. Can you check of this is what causing the problem?

The variables don’t look right. It should look like this: https://gist.github.com/anonymous/394106f4252fe55e0ee7d9b62e131d9e

Thanks it solved the problem

Apologies for reviving this, however I’m curious to understand the logic behind why it returned zero.

How does initialising the variable twice return zero?

Do the NextGuess/Update functions pull the value from the initialised declarations outside of the StartGame function, and as there is no value, assign it zero?

And does this mean that by only assigning values to the variables within the StartGame function, that the initialised variables outside of it can see those values? In my head, this would explain how the NextGuess and Update functions know what value to return, since they cannot se them within another function…

I hope this makes sense…

What happened here is that the variables were declared twice in different contexts. First they were declared as global variables, i.e. outside of any functions. All functions inside the same class have access to those global variables. Since they weren’t explicitly initialized, C# assigns them default values (for ints, this value is 0).

When the keyword int was used in the Start() method, instead of initializing those global variables, new local variables (i.e. only accessible within Start()) were created with the same name, and initialized. The compiler just took those local variables as completely separate entities to the previously declared global ones.

I hope that answers your question.