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

So why does this work?

After talking about scope, shouldn’t max equaling 1001 be destroyed after walking out the Start function? So Update wouldn’t see the variable as 1001 but rather as 1000 as it’s declared in the main class? Why does it work instead of us having to put the max = max + 1 at the top of the Update function (I understand why we can’t put it in the class)?

Hi,

Have you already tried to do the maths? Make the computer “guess” 5. Your min is 1, and max is 5.

min = 1
max = 5
guess = (min + max) / 2 = (1 + 5) / 2 = 3

min = 3
max = 5
guess = (3 + 5) / 2 = 4

min = 4
max = 5
guess = (4 + 5) / 2 = 4

Integers don’t support decimals, so everything behind the dot gets cropped.

Did this clear it up for you?

Hey Nina,

Thanks so much for your response. :slight_smile:

I understand that perfectly, so not a problem. What I was querying was why we put (max = max + 1) in the Start function instead of the Update function because I was thinking of scope but I kinda figured out where I went wrong with my thinking. And that was because I was replying to you so you still helped. Thanks! :smiley:

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

Privacy & Terms