Can someone explain the difference between these two codes?

Hi guys, first time poster. I’m very new to both Unity and C# so forgive me if what I’m asking is very basic or if I’m missing something very obvious but I simply cannot understand why the two codes below work differently from one another. I’m currently doing the Unity Learn Essentials Pathway and one of the final missions is to implement something original and unique to the final scene. I came up with the idea of implementing an “On/Off” button for the vacuum cleaner and later adding a unique sound for each switch.

The first code I wrote to implement my idea was as follows (Only typing the relevant parts):

bool power = false; // Starting the vacuum cleaner in “Off” mode

void Update()
{
// If vacuum cleaner is turned on, then turn it off after pressing the “O” button.

if (power == true && Input.GetKeyDown(KeyCode.O))
{
power = false;
}

// If vacuum cleaner is turned off, then turn it on after pressing the “O” button.

if (power == false && Input.GetKeyDown(KeyCode.O))
{
power = true;
}
}

Implementing the above code produces a strange outcome. The code only allows me to turn the vacuum cleaner on but doesn’t let me turn it off. If I start the vacuum cleaner in off mode (i.e. power = false) then I can turn it on in the game by pressing “O” but I can’t turn it off again, and if I start the vacuum cleaner in on mode (i.e. power = true) then it permanently stays on and never responds to me pressing “O” to turn it off. I played around with the code and realized that I could make it work the way I wanted by rewriting the code as follows:

bool power = false; // Starting the vacuum cleaner in “Off” mode

void Update()
{
// If the “O” button is pressed, check to see whether power is “On” or “Off”, then switch power.

if (Input.GetKeyDown(KeyCode.O))
{
if (power == true)
{
power = false;
}
else
{
power = true;
}
}
}

This second block of code works exactly the way I want. I can now turn the vacuum cleaner both on and off anytime by pressing “O”. What I don’t understand is that why does the first block of code not work and the second one works? What is happening in the first block that is preventing me from switching power to false by pressing “O”?

This is a perfect example where the debugger will help you see why the first code behaves as it does. You can see your code execute line by line, and how variables change their values.

When O is pressed, it sets power to false but the next statement checks if power is false, which it now is and also O is still pressed, thus it sets power to true again all within a single Update call.

1 Like

Oh now I understand! The “O” key is considered “Pressed” for the entire block of code in “Update()” and not just for the first relevant line of code. Now it makes sense. Thank you for the clarification.

Welcome.

FYI: Here’s how to post code on the forums: Using code tags properly

1 Like

Your code is simply a toggle logic which can be simplified as

if (Input.GetKeyDown(KeyCode.O))
    power = !power;
1 Like