Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • If, Then, Otherwise
  • Comparison Operators
  • Using Comparison Operators
  • Switch / Case Statements

Was this helpful?

  1. Coding Basics

Conditionals (If / Then / Else)

If, Then, Otherwise

Most code can be boiled down to "If this is true, do this."

First, let's look at how to compare values to see if something is true.

Comparison Operators

Operator

Name

Example

==

Equals

x == y

===

Equals (exact)

x === y

!=

Does Not Equal

x != y

>

Greater Than

x > y

<

Lesser Than

x < y

>=

Greater Than or Equal To

x >= y

<=

Lesser Than or Equal To

x <= y

When comparing using an equals sign, remember to use two equal signs. Using only one tells the program to store/overwrite a variable instead of comparing it.

Using Comparison Operators

An if statement asks if a comparison is TRUE.

if (true) 
{
    // Do something
}

The commands in the brackets will only run if the comparison in the parentheses is true.

Example of true statements:

int x = 23;

// Example of true statements

if (x == 23) { }
if (x != 2) { }
if (x < 24) { }
if (x >= 23) { }

You might want the program to so something by default or when the comparison is false. Use an if/else statement by using the keyword "else" followed by commands in curly brackets:

if (true)
{
    // Do something
}
else
{
    // Do something else
}

The commands in the else brackets will only run for the times the comparison is false. Writing this code outside of an else statement will apply it in all cases.

There are also ways to do this while checking for a few different comparisons, using an if/else if/else statement:

Format:

if (true)
{
    // Do something
}
else if (true)
{
    // Do this other thing
}
else
{
    // Do something else
}

Example:

string fruit = "apples";

if (fruit == "apples")
{
    print("An apple a day keeps the doctor away!");
}
else if (fruit == "bananas")
{
    print("This is bananas!");
}
else
{
    print("That sounds tasty.");
}

There can be multiple else if statements.

Note: Each comparison goes in order, so if a comparison is true in the first if statement, it does not get checked in the else if and else statements that follow. Code outside of the if/else if/else statement will be applied to all cases.

Switch / Case Statements

Using else if statements can get tedious, especially when you are comparing something to a specific number or string.

A switch/case statement takes in the variable to compare and compares it to defined "cases," running the commands within those cases. Instead of an else statement, a default case can be defined.

Format:

switch (value)
{
    case comparedValue1:
        // Do something
        break;
    case compareValue2:
        // Do another thing
        break;
    default:
        // Do something if it all else is false
        break;
}

Example:

string fruit = "apples";

switch (fruit)
{
    case "apples":
        print("An apple a day keeps the doctor away!");
        break;
    case "bananas":
        print("This is bananas!");
        break;
    default:
        print("That sounds tasty.");
        break;
}
PreviousVariablesNextArrays

Last updated 4 years ago

Was this helpful?