Ternary Operator

Tutorial

·

intermediate

·

+10XP

·

5 mins

·

(2156)

Unity Technologies

Ternary Operator

How to utilize the Ternary Operator to build simple, shorthand IF-ELSE logical conditions.

1. Ternary Operator

TernaryOperator

using UnityEngine;
using System.Collections;

public class TernaryOperator : MonoBehaviour 
{
    void Start () 
    {
        int health = 10;
        string message;

        //This is an example Ternary Operation that chooses a message based
        //on the variable "health".
        message = health > 0 ? "Player is Alive" : "Player is Dead";
    }
}

Complete this tutorial