Unity – IF Statements (Conditionals)

It was a busy Monday in Unity, so there are quite a few things to cover this week. However the main focus of the lesson was understanding IF statements. While my issues with programming usually boil down to unfamiliarity with the syntax, at least I already understand the purpose of conditionals and already made use of them in my walking simulator to operate the boors (along with a whole slew of booleans).

The word conditional as explained by dictionary.com:

“adjective

1.

imposing, containing, subject to, or depending on a condition or conditions; not absolute; made or allowed on certain terms:

conditional acceptance.

2.

Grammar. (of a sentence, clause, mood, or word) involving or expressing a condition, as the first clause in the sentence If it rains, he won’t go.

3.

Logic.

  1. (of a proposition) asserting that the existence or occurrence of one thing or event depends on the existence or occurrence of another thing or event; hypothetical.
  2. (of a syllogism) containing at least one conditional proposition as premise.”

This description is no different in programming. If a condition is met, the code following will run. If the player has a door key, open door.

There are three parts to an IF statement:

  1. The keyword IF
  2. Condition (in brackets)
  3. Body {in curly brackets}

As an example:

if (number ==1) {

Debug.Log("Heads");

}

The explanation surrounding the example was that of a coin flip and a number generator. The number generator either gave a 1 or a 2 (heads or tails).

Now that the theory is out of the way. Onto its use in our ongoing asteroids game.

leveltimer

The first use of IF in our asteroids game, a level timer. A fairly standard feature in a lot of games and a nice bit of code that can be reused in future projects. A new public variable called time was declared earlier, using a float and set to 5 seconds. Since this was public the time amount could be altered from scene to scene. The IF statement then just calls on the scene manager which we have used in previous weeks.

There was a brief moment in class when I’d finished this chunk of the exercise and had a moment of spare time. Diving into the official Unity Roll a Ball tutorial with my wife we tried to get this time variable to display on the UI. It would be unfair to give a restriction the player and then withhold that information from them. We made it so far, getting a text box for it to print to and a SetTimer function and while this printed to UI, it did so with about 8 decimal spaces. Thankfully after a quick plead to Ant for help, he demonstrated how to round the number down. This is the finished script.

timerstring

The variable that went alongside this was ‘public Text timeText;’.

The last addition to this lesson was pickup objects. Fantastic! Alongside obstacles, a goal, a time limit and certain player death conditions this almost makes a full framework for a simple game!

This was delivered in a video tutorial and while I hit a snag halfway through and got frustrated, it turned out that my old nemesis the misplaced bracket struck again. Once that was fixed it was plain sailing and introduced the concept of IF/ELSE statements rather than simply IF. If condition is met do one thing, otherwise do another.

pickups

In this instance if the player collides with and collects the pickup they can progress to the goal and move to the next level otherwise the goal will not become active. For this to happen the starCollected boolean must be true. To test this I had to change the level timer to ten seconds, it was nearly impossible to do in five unless I had lined up the star and the goal in a straight line.

pickup

Last but certainly not least we briefly covered Prefabs in Unity. The idea behind this is making complex assets easy to re-use and duplicate across scenes in the project. For example our ship now has a rather lengthy script attached and its colliders, copying and pasting this across scenes could get messy and if you need to make a change, you would be required to make this change individually across each copy of the asset.

Turning the ship into a prefab means that all copies of the asset can be updated at once. Having access to all your pre-built complex assets means you can block out levels way faster once all the prep work has been done too. A feature well worth keeping in mind once we start on bigger projects or game jams.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s