Introduction
This article demonstrates how to work with C# Operators in Unity.
Prerequisites
Unity Environment version 5.6.1
Once again, refer to my previous article to get started.
- Scripting with C# in Unity
Operators in C#
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.
“+” Operator
Open mono development-unity. Type the code like the following.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class MyScript : MonoBehaviour {
-
- public int myNumber = 5;
-
-
- void Start () {
- myNumber = myNumber + 5;
- print (myNumber);
- }
-
-
- void Update () {
-
- }
- }
Save the program.
Go back to Unity. The coding will be changed. Press the “Play” button and go to console. The output will be displayed.
“-” Operator
Go to the mono development-unity. Type the coding same as + operator. Change the values + 5 into – 3 and save the program.
Go back to Unity. Press the “Play” button and go to the Console. The output will be displayed.
“*” Operator
Go to mono development-unity. Type the coding same as + operator. Change the values - 5 into * 3 and save the program.
Go back to Unity. Press the “Play” button and go to Console. The output will be displayed.
“/” Operator
Go to mono development-unity. Type the coding same as + operator. Change the myNumber as 10. Give the input value as *3 to /2 and save the program.
Go back to Unity. Press the “Play” button and go to Console. The output will be displayed.
Go to mono development-unity. Type the coding same as + operator. Change the myNumber as 7. Give the input value from /2 to /3 and change the data type to float. Save the program.
Go back to Unity. Select the Main camera. Go to the inspector window, select MyScript, and click on the Reset option. Press the “Play” button and go to console.
The Output can be displayed.
“%” Operator
Go to the mono development-unity. Type the coding same as + operator. Change myNumber as 7. Give the input value as % 3 and save the program.
Go back to Unity. Press the “Play” button and go to console. The output will be displayed.
Go to the mono development-unity. Type the coding same as + operator. Change myNumber to 5. Give the input as myNumber +=5; Save the program.
Go back to Unity. Press the “Play” button and go to the Console. The output will be displayed.
“++” operator
Go to the mono development-unity. Type the same as + operator. Change the myNumber to 5. Give input as myNumber++; in the start method. Save the program.
Go back to Unity. Press the “Play” button and go to Console. The output "6" will be displayed.
Go to the mono development-unity. Type the coding same as + operator. Change myNumber as 5. Give the input as myNumber++; in the update method and save the program.
Go back to Unity. Press the “Play” button and go to Console. The output will be displayed in continuously add one number.
Summary
I hope you understood how to work with C# Operators in Unity.