Sitemap

Back to Basics: If statements, Unity.

3 min readAug 11, 2025

--

Press enter or click to view image in full size

When making games in Unity, your scripts often need to make decisions — like “if the player has enough points, open the door” or “if health is zero, end the game.”

This is where if statements come in. They let your code check a condition and run certain lines only if that condition is true.

Basic structure of an if statement.-

Often you can also find “else”

Press enter or click to view image in full size

And also “else if”

Press enter or click to view image in full size

Basically what “else if” means in regular language — “if the first thing didn’t happen, then check this one instead.”

Get Daniel Marabotto’s stories in your inbox

Join Medium for free to get updates from this writer.

Using the example above, we want to check the average of 5 test and depending on the average, the student will get a mark (A,B,C or F)

First, we need to declare the variables for the tests and average.-

With SerializeField, we can change the values in the inspector

Then we can make a method.-

Then we can call that method in Update()

Press enter or click to view image in full size

We can change the values in the inspector and see the message in the Console.-

Press enter or click to view image in full size
Press enter or click to view image in full size

If statements are like the decision-makers in your Unity scripts — they help your game react to different situations instead of doing the same thing every frame. By checking conditions and running only the code that applies, you can create smarter, more dynamic gameplay. Whether it’s grading a student, opening a door, or triggering a win screen, If statements are a simple but powerful tool every Unity developer should master.

--

--