---
title: "Ternary Operator"
content_type: tutorial
url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/ternary-operator?language=en"
language: en
container_type: course
container_title: "Intermediate Scripting"
container_url: "https://learn.unity.com/course/intermediate-gameplay-scripting.md?language=en&version=2019.3"
standalone_markdown_url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=2019.3"
previous_title: "Creating Properties"
previous_url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/creating-properties?language=en&version=2019.3"
previous_markdown_url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/creating-properties.md?language=en&version=2019.3"
next_title: "Statics"
next_url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l?language=en&version=2019.3"
next_markdown_url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2019.3"
difficulty: intermediate
duration_minutes: 5
unity_version:
  - "2019.3"
  - "2019.2"
  - "2019.1"
  - "2018.4"
  - "2018.3"
  - "2018.2"
  - "2018.1"
  - "2017.4"
  - "2017.3"
  - "2017.2"
  - "2017.1"
  - "5.x"
  - "4.x"
supported_unity_versions:
  - name: "2021.3"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=2021.3"
  - name: "2019.3"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/ternary-operator.md?language=en&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/ternary-operator.md?language=en&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/ternary-operator.md?language=en&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/ternary-operator.md?language=en&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/ternary-operator.md?language=en&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/ternary-operator.md?language=en&version=4.x"
last_updated: "2026-08-04T14:22:05.000Z"
---

# Ternary Operator

Use the ternary operator to write concise IF-ELSE conditions in your C# logic.

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

## 1. Ternary Operator

[Video](https://connect-mediagw.unity.com/h1/20220110/videos/21b17c5a-fcc6-4e87-b939-72a31d820778_U_Intermediate_2_TernaryOperator.mp4) ([Transcript (en)](https://connect-mediagw.unity.com/h1/20220111/2772ff97-f897-4c79-9397-bb7f502cdca2_I_02_Ternary_Operator_V2.0.vtt))

**TernaryOperator**

```csharp
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";
    }
}
```
