Day 8 — Getting familiar with if statements in Unity

Connor Fullarton
4 min readMar 22, 2021

--

Hey and welcome!

Let’s talk about the legendary if statements.

You’ll tend to find if statements in most languages and they work off of if/then logic where the if statement will only run if the logic is true. So it’s along the lines of “if this statement is true then run this code”.

if (5 > 2)
{
//Go ahead and run this code
}

In the example above 5 is greater than 2 so it will then run whatever code you have contained within the curly braces. An example of where this comes in handy could be an fps game, let’s say you have a gun with a totalAmmo variable in it you’ll want to be making sure that the player can only fire the gun if the ammo count is higher than 0 so you’d have something like this:

if (totalAmmo > 0)
{
// Run the code to fire the gun
}

Pretty handy right? There’s also an else if statement you can use but only if you’re checking the same condition. So let’s say we also need to check when our totalAmmo hits 0 so we can tell Unity to stop firing the gun we could have it looking like this:

if (totalAmmo > 0)
{
// Run the code to fire the gun
}
else if (totalAmmo == 0)
{
// Run the code to stop the gun from firing
}

This works because totalAmmo is still the condition we’re checking here, if we had something else like totalHealth and put it in the condition in our else if that wouldn’t work.

Then we have the else statement, this one will run if the condition in your if statement comes up false, so we can actually use this to improve our code here:

if (totalAmmo > 0)
{
// Run the code to fire the gun
}
else
{
// Run the code to stop the gun from firing
}

Our if statement here is only true while the totalAmmo is greater than 0, that means when it reaches 0 the condition will be false and the code will go straight to our else statement to run that.

Adding to our game

Now let’s see how we can put this knowledge to use in our 2D game.

Currently we have no boundaries in our game which means we can move our player beyond the camera’s view into the great beyond. What I want to do here is to make it so that our player will wrap along the x axis, so if we were to move all the way to the left the player would find themselves on the right side of the screen and vice versa.

In reality here we’re just going be changing our players x position from say -10 to 10 so nothing fancy in reality but we’ll be needing if statements to make it happen.

if (transform.position.x >= 10f){   transform.position = new Vector3(-10f, transform.position.y, 0);}else if (transform.position.x <= -10f){   transform.position = new Vector3(10f, transform.position.y, 0);}

So there’s the code, now if you’re wondering what transform.position.x is well let’s think about it logically. When we type transform we’re telling our code to look for the transform component in our player object, then we’re using the . operator to access the position in the transform and then again to access the x value in the position. This should sound familiar because you can see it in action in the player’s inspector window in Unity:

So if that transform.position.x value ends up being greater than or equal to 10 then the if statement condition will be true and run the code inside. The code inside should be looking familiar to you but one thing I want to mention about it.

transform.position.x = -10f;

If you were to try the above here you would find that it’s not working even though nothing looks wrong with the code itself. Well the problem here is that the y and z values are being ignored which isn’t handy since we’re dealing with a 3D space in our game so Unity doesn’t know what to do about the y and z values of our player object if we don’t specify it ourselves.

Armed with that knowledge and making sure that your code is in the Update() method since this is something we need to check on every frame you’ll end up having something looking like this:

There will be plenty to do with if statements in the future so it’s important to nail down the syntax now of if(CONDITION) {Run Code} as early as we can.

--

--

Connor Fullarton
Connor Fullarton

Written by Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

No responses yet