[Solved] Statement works fine without being in curly brackets

Hi all,
I’ve tried not to use ( to be honnest, it was a mistake) the curly brackets for the statement and the code run fine. At play mode, at first I can see the “Hello World” and if I press space, the “you are …cell”.
Could someone explain if a statement can exist without the curly brackets?
Here s’ my code:

public class TextController : MonoBehaviour {

public Text text;

// Use this for initialization
void Start () {
	text.text = "Hello World";
}

// Update is called once per frame
void Update ()
{
	if (Input.GetKeyDown (KeyCode.Space)) 
		text.text = "You are in a prison cell";

	
}

}

Same question here but it is not solved

1 Like

You can do it this way when you only have a single statement after your if(). The compiler treats it like a single-line statement.

However if you added a second statement that also happened when space was pressed, you’d need the curly braces. Many devs use the curly braces every time anyway, to aid readability.

3 Likes

Merci James :slight_smile:

1 Like

Privacy & Terms