Switch Statements

Tutorial

·

Beginner

·

+10XP

·

5 mins

·

(2279)

Unity Technologies

Switch Statements

Switch statements act like streamline conditionals. They are useful for when you want to compare a single variable against a series of constants. In this video you will learn how to write and use switch statements.

This tutorial is included in the Beginner Scripting project.

Previous: Enumerations

Next: This is the final tutorial in the project.

Languages available:

1. Switch Statements

ConversationScript

using UnityEngine;
using System.Collections;

public class ConversationScript : MonoBehaviour 
{
    public int intelligence = 5;
    
    
    void Greet()
    {
        switch (intelligence)
        {
        case 5:
            print ("Why hello there good sir! Let me teach you about Trigonometry!");
            break;
        case 4:
            print ("Hello and good day!");
            break;
        case 3:
            print ("Whadya want?");
            break;
        case 2:
            print ("Grog SMASH!");
            break;
        case 1:
            print ("Ulg, glib, Pblblblblb");
            break;
        default:
            print ("Incorrect intelligence level.");
            break;
        }
    }
}

Complete this tutorial