C# Conditional Statements In Unity

Introduction

 
This article demonstrates how to work with C# conditional statements in Unity.
 
Prerequisites
 
Unity Environment version5.6.1
 
Once again, refer to my previous article to get started.
  • Scripting with C# in Unity
Conditional statements
 
If statement
 
Step 1
 
First, you have to open the Unity project. Create terrain, trees, and water. Add skyboxes in your project. Create C# scripts and rename the script as MyScript.
 
'
 
Go to the mono-develop and write the code like the following.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class MyScript : MonoBehaviour{  
  6.   
  7.     public int myNumber = 10;  
  8.   
  9.     // Use this for initialization  
  10.     void Start () {   
  11.         if (myNumber > 5) {  
  12.             print ("myNumber is greater than 5");  
  13.         }  
  14.     }  
  15.     // Update is called once per frame  
  16.     void Update () {  
  17.           
  18.     }  
  19. }  
Go back to the Unity window. Click on the Main camera. Select "My Script" and click on the Reset option.
 
 
Click on the “Play” button. The output will be displayed.
 
 
Change the input value >15 and print the statement like in the following code.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class MyScript : MonoBehaviour{  
  6.   
  7.     public int myNumber = 10;  
  8.   
  9.     // Use this for initialization  
  10.     void Start () {   
  11.         if (myNumber > 15) {  
  12.             print ("myNumber is greater than 15");  
  13.         }  
  14.     }  
  15.     // Update is called once per frame  
  16.     void Update () {  
  17.           
  18.     }  
  19. }  
 
Go back to the Unity window. Click on the “Play” button. Nothing will be displayed in the console because the value is not greater than 10.
 
 
Give the input value as <=11 and print the value.
 
 
The output will be displayed in the console.
 
 
Give the myNumber as 11 and input value as != 10; print the value that is not equal to 10.
 
 
The output will be displayed.
 
 
if-else statement
 
Step 2
 
Go to the mono-develop and type the following code.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class MyScript : MonoBehaviour{  
  6.   
  7.     public int myNumber = 10;  
  8.   
  9.     // Use this for initialization  
  10.     void Start () {   
  11.         if (myNumber == 10) {  
  12.             print ("myNumber is equal to 10");  
  13.         }  
  14.         else {  
  15.             print ("myNumber is not equal to 10");  
  16.   
  17.         }  
  18.     }  
  19.     // Update is called once per frame  
  20.     void Update () {  
  21.           
  22.     }  
  23. }   
Save the program.
 
 
Go back to the Unity window. Reset "My Script".
 
 
Click on the "Play" button. The output will be displayed into equal.
 
 
Change the value as 11 and click on the Play button. The output will be displayed as not equal.
 
 
else if statement
 
Step 3
 
Go back to the mono to develop and type the following code.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class MyScript : MonoBehaviour{  
  6.   
  7.     public int myNumber = 10;  
  8.   
  9.     // Use this for initialization  
  10.     void Start () {   
  11.         if (myNumber == 10) {  
  12.             print ("myNumber is equal to 10");  
  13.         }  
  14.         else if (myNumber == 15) {  
  15.             print ("myNumber is equal to 15");    
  16.   
  17.         }  
  18.         else {  
  19.             print ("myNumber is not equal to 10");  
  20.   
  21.         }  
  22.     }  
  23.     // Update is called once per frame  
  24.     void Update () {  
  25.           
  26.     }  
  27. }  
Save the program.
 
 
Reset "My Script" and click on the Play button. The output will be displayed in the if statement.
 
 
Give the value as 12. The output is displayed in else if statement.
 
 
Give the value as 15. The output will be displayed in else statement.
 
 

Summary

 
I hope you understood how to work with C# conditional statements in Unity.


Similar Articles