make this change in your function
public void OnPressHigher()
{
min = guess + 1;
if (min <= 1000) {
NextGuess();
}
make this change in your function
public void OnPressHigher()
{
min = guess + 1;
if (min <= 1000) {
NextGuess();
}
thank you! this helped a lot
Better to change the code to something like this:
public void OnPressHigher()
{
min = guess + 1;
if ( min >= max)
{
min = max;
}
NextGuess();
}
Reasons: do not depend on fixed numbers but use the variables (because range may change), and second to keep the code flow the same for both cases.
This worked! Thanks. I came on here to see if there was a solution. I tried a few things that didn’t quite work.
Thanks for this solution. I too noticed it was going higher than 1000. I’m glad this solution has prevented that.
Thank you guys, this work)))
I found a (in my opinion) more elegant solution (but probably also more complex to understand for complete beginners):
Add the using Unity.Mathematics
to the top of the file
Change your OnPressHigher()
and OnPressLower()
to this:
public void OnPressHigher()
{
min = math.min(guess + 1, max);
NextGuess();
}
public void OnPressLower()
{
max = math.max(guess - 1, min);
NextGuess();
}
math.min()
takes two numbers and returns the lowest. This means that min
will now never be higher than max
math.max()
does the opposite. It takes two numbers and returns the highest. This means that max
will now never be lower than min
This can also be a good solution:
if (guess < max)
{
min = guess + 1;
NextGuess();
}
Thank you for adding this! I might be the only one retaking this course, so I doubt there will be an update to the video, but I appreciate that other people had the same problem and posted solutions.