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.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class MyScript : MonoBehaviour{
-
- public int myNumber = 10;
-
-
- void Start () {
- if (myNumber > 5) {
- print ("myNumber is greater than 5");
- }
- }
-
- void Update () {
-
- }
- }
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.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class MyScript : MonoBehaviour{
-
- public int myNumber = 10;
-
-
- void Start () {
- if (myNumber > 15) {
- print ("myNumber is greater than 15");
- }
- }
-
- void Update () {
-
- }
- }
-
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 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.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class MyScript : MonoBehaviour{
-
- public int myNumber = 10;
-
-
- void Start () {
- if (myNumber == 10) {
- print ("myNumber is equal to 10");
- }
- else {
- print ("myNumber is not equal to 10");
-
- }
- }
-
- void Update () {
-
- }
- }
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 into not equal.
else if statement
Step 3
Go back to the mono develop and type the following code.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class MyScript : MonoBehaviour{
-
- public int myNumber = 10;
-
-
- void Start () {
- if (myNumber == 10) {
- print ("myNumber is equal to 10");
- }
- else if (myNumber == 15) {
- print ("myNumber is equal to 15");
-
- }
- else {
- print ("myNumber is not equal to 10");
-
- }
- }
-
- void Update () {
-
- }
- }
Save the program.
Reset "My Script" and click on the Play button. The output will be displayed in 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.