Problems with if-statements (C#)

Hello. I am making a game, my first proper game, and I have some problems. The player is an object that can change properties and material when a key is pressed.

It works fine enough, when I just have one double-if statement - even though it doesn’t detect every key-hit, sometimes I have to press the key twice (I don’t know why). But when I have one double-if statement for each playerstate (true/false), it doesn’t work. This is part of my script:

void FixedUpdate (){
		if (standardPlayer == true) {
			if (Input.GetKeyDown(KeyCode.Q)){
				standardPlayer = false;
				Renderer rend = GetComponent<Renderer>(); rend.material = materials[1];
			}
		}
		if (standardPlayer == false) {
			if (Input.GetKeyDown (KeyCode.Q)) {
				standardPlayer = true;
				Renderer rend = GetComponent<Renderer> (); rend.material = materials[0];
			}
		}
	}

The full class script can be found here: using UnityEngine;using System.Collections;public class PlayerControll : M - Pastebin.com

Any idea how I can fix it, so the player can toggle between the two states when he so desires? Also, is it best to place it in update or fixedUpdate?

Thanks in advance

Hi there @Chrras

When it comes to checking for input from the user it’s best to place it within the Update() method rather than the FixedUpdate() method as FixedUpdate is only called every physics step and so the user key presses will sometimes be missed, hence why you sometimes have to hit the key twice for it to register. Also, FixedUpdate should only be used for physics calculations or mathematical algorithms that require a fixed frame rate.

As for the if statements. When you are comparing something to be either true or false you can make use of the “else” keyword. This keyword will cause the block to of code to be executed if the first conditional statement fails. So for example, consult below:

	void Update ()
	{
		if (standardPlayer == true) 
		{
			if (Input.GetKeyDown(KeyCode.Q))
			{
				standardPlayer = false;
				Renderer rend = GetComponent<Renderer>(); rend.material = materials[1];
			}
		}
		else
		{
			if (Input.GetKeyDown (KeyCode.Q)) 
			{
				standardPlayer = true;
				Renderer rend = GetComponent<Renderer> (); rend.material = materials[0];
			}
		}
	}

The code above will now check to see if standardPlayer is true. If not, it will then check to see if standardPlayer is false. Now, when you press the q key it will change the material and the standardPlayer variable so that next time, the other condition will become true.

If you were to not do this, then every time you press the Q key, it will evaluate both conditions immediately as it would check the first if statement, and then it would proceed to check the next if statement as well regardless of the outcome from the first, which by this point will have become false due to the first one and thus it looks like the material never changed at all, when it in fact did, it just changed too fast to notice.

I’m sorry if this explanation got lengthy, if you need further information or this didn’t solve your problem feel free to let me know. I hope this helps! :slight_smile: