C# Operators In Unity

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. 

  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class MyScript : MonoBehaviour {  
  6.   
  7.     public int myNumber = 5;  
  8.   
  9.     // Use this for initialization  
  10.     void Start () {   
  11.         myNumber = myNumber + 5;  
  12.         print (myNumber);  
  13.     }  
  14.   
  15.     // Update is called once per frame  
  16.     void Update () {  
  17.   
  18.     }  
  19. }   

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.