Back to Basics: If statements, Unity.
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”
And also “else if”
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.-
Then we can make a method.-
Then we can call that method in Update()
We can change the values in the inspector and see the message in the Console.-
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.









