C# If-Statements in Unity

How to use If-statements in Unity

Tristan Engel
3 min readMay 1, 2021

In my previous article we talked about variables. This time we talk about if-statements. These are another fundamental concept of programming applicable to all languages.

What are If statements?

Computers will only do what they are told. With variables we tell them to store information in ‘boxes’. If statements are what allows us to let the computer make decisions based on conditions.
It’s made of two parts: if and then. If condition is met, then do X.

An example in pseudocode would be:
If shoot button is pressed
then shoot laser

This opens up endless possibilities. From sending an automated email at a specific time or creating a bot that plays your game automatically to play test. If statements can be put in each other and create more complex decisions.

Example of nested if statement:
If you are hungry
then If you got money
— then buy food

How to use an If-statement in Unity?

The previous section demonstrated how the logic of the if statements work. Here we’ll cover how to actually use them in an Unity script. Let’s take an example in pseudocode and translate it.
If you’re sad
then eat candy
Let’s put it into code.

== means equals, = means becomes

Our next example we use will be a simple check to see if you can drive. This time we check with a negative if question.
If not drunk
then say you can drive

!= means NOT equal

Multiple conditions AND, OR

When using if statements it’s possible to check for multiple condition at once. AND — Only when both (or more) conditions are met, the then-part is executed. Ex. If you are a swordsman AND if you got a sword equipped you can attack with the sword.

Use && to check for multiple conditions in a single if statement

OR — If either condition is met, the then-part is executed. Ex. If enemy is shot in the head OR in the heart, then enemy is dead.

Use || to check if either conditions are met

Else

There also is an else statement. An example would be to eat ice when temperature is high and when the temperature isn’t high enough (condition not met) then we do what is put after else. In pseudocode it looks like this:
If temperature is high
then eat ice cream
else
then eat cake

Else example in code

Else if

Now there’s also an Else if statement. Let’s take the previous example and expand it by saying we drink hot coco when the temperature is low. To put it in pseudocode:
If temperature is high
then eat ice cream
else if temperature is low
then drink hot coco
else
then eat cake

If, else if, else example in code

There are endless combination you can create with these three basic if statements. There’s just no ‘else’ if there is no ‘if’. Remember to keep it readable.

Flow chart of how multiple if statements could be combined

--

--

Tristan Engel
Tristan Engel

Written by Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

No responses yet