Max = max + 1 in void Start - why does this work?

I am trying to understand why setting the max to max+1 within Start fixed the bug? That value is supposed to be only within Start method right?

Which lecture are you referring to

Im refering to the game wizard of the course of 2D, a video with the name of calculate the guess variable
thank you for your response :smiley:

(I’m just starting C# myself, so I think this is right! Just have a bit of computer logic learning in my background.)

It has to do with the partitioning of the Method and Class boxes that he talks about in the video.

The Start runs through the code in order one time. So it’ll print out via Debug.Log “The highest number is 1000” and then at the end, tell the variable in the Class to set itself max + 1

So, hidden, the ‘player’ sees to guess between 1 and 1000, but in the program, max is set to 1001.

This only applies if the user chooses 1000 as their number, so only the Up arrow is pressed. So the Update Method never overwrites the Max to a lower number than 1001 (as in the case when the down arrow is pressed.)

The math says (Max + Min) / 2, but that is also rounded since it’s an integer.

The way the bug works out, if the player’s number is 1000, is (1000 + 999) / 2 = 999.5, which is rounded down to 999 - hence the bug.

Setting the max to + 1 in Start makes the hidden max variable 1001, so the final calculation is (1001 + 999) / 2 = 1000.

A Start Method code can influence a Class variable, but it cannot influence an Update Method variable directly according to the video. So using Start to update the Class variable once, allows the Update Method to correctly calculate a guess of 1000.

If it was set in the update, it’d likely add +1 to the max every update, which would get a little out of hand. (Just tested it myself, it guesses over 1000… a LOT :laughing: )

Hope that’s clear and helps!

1 Like

Thanks for your comment MatthewE!

It confirmed me if it works like that.
And also I tried write statement (max = max + 1) into update, to see what it does. XD

Here is effect…

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms