Show Menu
Cheatography

C# & Unity MonoBehaviour Cheat Sheet by

Basic keywords for Unity scripting

C#

using
Include namespace
class
Define name
// comment
Comment: Same line
/* comment */
Comment: Multiple lines
private, protected, public
Access specifiers
bool
Variable: true or false
int
Variable: Integer number
float
Variable: decimal number
string
Variable: Text
null
Empty nothing
+ - * / % ++ --
Math functions
if(condition){code}...else if(condition){code}...else {code}
Condit­ional statements
== != > <
Relational operators
&& ||
Logical operators
switch­(){case var: code break; default: code break;}
Similar to if condit­ional statement
Expression ? true: false;
Similar to if
while(­con­dit­ion){code}
Loop: while
for(init;condition;increment) {code}
Loop: for
do{code}while(condition)
Loop: do...while
int[] vars = new int[10];
Array wich contains 10 integers
vars[0] = 2;
Assign to an index of an array
float[] vars = new int[2]­{10­f,20f};
Another array
Methods
public void DoSmth(){code}
Function definition
private void Add(int num1, int num2){code}
Function with parameters
private int Sum(){code return int;}
Function with return type
 

MonoBe­haviour

Awake()
Called once before everything else
OnEnable()
When a GameObject becomes active
Start()
Called once after the Awake, before the Update
Update()
Called every frame. Use Time.d­elt­aTime for time relevant code
FixedU­pdate()
Called every fixed framerate frame. Used when dealing with Rigidbody.
Instan­tiate
Creates a copy of the original object
Destroy
Remvoes a gameob­ject, or component.
GetCom­ponent <Type>()
Returns the component of Type if the game object has one attached
gameob­jec­t.t­ran­sfo­rm.p­os­ition
Vector3 Position of a GameObject
gameob­jec­t.t­ran­sfo­rm.r­ot­ation
Quaternion rotation of a GameObject
Quater­nio­n.Euler
Returns the quaternion of euler rotation
 

Other Components

Collider
OnColl­isi­onE­nte­r(2D)
Called when this collid­er/­rig­idbody has begun touching another rigidb­ody­/co­llider
OnColl­isi­onE­xit(2D)
Called when this collid­er/­rig­idbody has stopped touching another rigidb­ody­/co­llider.
OnTrig­ger­Ent­er(2D)
Called when this Collider other enters a trigger Collider.
OnTrig­ger­Exi­t(2D)
Called when this Collider other has stopped touching a trigger collider.
isTrigger
Triggers collision without physics
Physic­s.R­aycast
Checks if there are colliders in a line
RigidBody
isKine­matic
Controls whether physics affects the rigidbody.
useGravity
Controls whether gravity affects this rigidbody.
AddForce
Applies a force of a vector to the rigidbody
AddTorque
Adds torque to the rigidbody
Other
Input.G­et­Key­Dow­n("F­ire­1")
Returns true or false for key
Lerp
Linear interp­olation between values in time from 0 to 1
Mathf
Math functions
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Ubuntu 16.04 Unity Tastenkombinationen Keyboard Shortcuts
          Unity 2D Basics Cheat Sheet