Unity3d Scripting Quick Reference
Unity3d Scripting Quick Reference
Unity3d Scripting Quick Reference
Arrays
string[] family;
family = new string[] {"Homer","Marge","Bart","Lisa","Maggie"};
Debug.Log("Family members="+family.Length);
Debug.Log("First members="+family[0]);
foreach (string name in family) {
Debug.Log(name);
}
Classes
public class ClassName : ClassNameExtends {
}
Shortcut
x=x+1
x++
y=y-1
y--
x=x+y
x+=y
x=x-y
x-=y
Conditionals
if (condition1) {
// do this
} else if (condition2) {
// do that
} else {
// do default
}
Logic
Logic
Symbol
Equal
==
Not Equal
!=
Greater Than
>
>=
Less Than
<
<=
And
&&
Or
||
switch(name) {
case "brian":
Debug.Log("Welcome Brian");
break;
case "will":
Debug.Log("Welcome Will");
break;
default:
Debug.Log("I dont know you");
break;
}
Loops
for (initialization; condition; increment) {
// actions
}
while (condition) {
// actions
}
1 of 2
Referencing Components
using UnityEngine;
using System.Collections;
gameObject.GetComponent<AudioSource>();
Instantiating Prefabs
You can dynamically create, or instantiate gameObjects in a
scene from other gameObjects or Prefabs:
}
}
Referencing GameObjects
2 of 2