Unity3D – C# – Basic Syntax
First you need call basic namespace and nest all the code inside a class with the name of your script, see the example below.
Basic Structure
Project Window> RMB> Create> C#> name it ‘MyFirstScript’> double click to open ‘MonoDevelop’ editor
// call basic namespace of Unity3D using UnityEngine; using System.Collections; // MonoBehaviour is the base class every script derives from // class nameofmyscript : derived from MonoBehaviour public class MyFirstScript : MonoBehaviour { // Unity3D exclusive function - Use this for initialization void Start () { }// END Start() // Unity3D exclusive function - Update is called once per frame void Update () { }// END Update() }// END of class MyFirstScript
Variables and Fuctions
// call basic namespace of Unity3D using UnityEngine; using System.Collections; // MonoBehaviour is the base class every script derives from // scope class nameofmyscript : derived from MonoBehaviour public class MyFirstScript : MonoBehaviour { // declare variables // scope type nameofvariable = value public int myInt = 5; // you can setup it inside inspector private int myInt2 = 6; // you can't setup it inside inspector // Unity3D exclusive function - Use this for initialization - void because it has no type void Start () { myInt = MultiplyByTwo(myInt); Debug.Log (myInt); }// END Start() // Your custom fuction // scope type functionname (type variable) public int MultiplyByTwo (int number) { int ret; ret = number * 2; return ret; } // Unity3D exclusive function - Update is called once per frame - void because it has no type void Update () { }// END Update() }// END of class MyFirstScript