Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
154 views

Unity3d Scripting Quick Reference PDF

C# and Unity provide common programming concepts like variables, data types, arrays, classes, functions, and control structures. Key Unity-specific aspects include using MonoBehaviour scripts to control game objects, referencing other game objects and components, and responding to common game events through callbacks. The Unity API exposes useful classes for game development involving things like GameObjects, transforms, physics, audio, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

Unity3d Scripting Quick Reference PDF

C# and Unity provide common programming concepts like variables, data types, arrays, classes, functions, and control structures. Key Unity-specific aspects include using MonoBehaviour scripts to control game objects, referencing other game objects and components, and responding to common game events through callbacks. The Unity API exposes useful classes for game development involving things like GameObjects, transforms, physics, audio, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C# Overview

Variables and Data Types Arrays


string[] family;
int i = 5; family = new string[] {"Homer","Marge","Bart","Lisa","Maggie"};
float x = 0.1f; Debug.Log("Family members="+family.Length);
bool isDone = false; Debug.Log("First members="+family[0]);
string name = "Brian" ; foreach (string name in family) {
int[] array1; Debug.Log(name);
int[] array2 = { 1, 3, 5, 7, 9 }; }

Vector3 loc = new Vector3(0,0,0);
Transform target; // or any other component type Classes
GameObject projectile; // can be set to a gameObject or Prefab public class ClassName : ClassNameExtends {
}
Variables and Scope
Functions
int p; // private member variable
private int p; // also private member variable int functionName(int x, int y) {
public int p; // public member variable // local variable
public static int p; // global member variable int result;
// actions
Assignment result = x + y;
// return from function
return result;
Expression Shortcut
}
x=x+1 x++
Conditionals
y=y-1 y--
if (condition1) {
// do this
x=x+y x+=y
} else if (condition2) {
// do that
x=x-y x-=y
} else {
// do default
}

Logic switch(name) {
case "brian":
Logic Symbol Debug.Log("Welcome Brian");
break;
Equal == case "will":
Debug.Log("Welcome Will");
Not Equal != break;
default:
Greater Than > Debug.Log("I don’t know you");
break;
Greater Than or Equal To >= }

Less Than < Loops


for (initialization; condition; increment) {
Less Than or Equal To <= // actions
}
And &&
while (condition) {
Or || // actions
}

1 of 2
C# and Unity API
Typical C# Script Template Referencing Components

using UnityEngine; You can get a reference to a component in the following ways:
using System.Collections;
Setup a public variable that holds the reference that you set in the
public class BasicTargetMover : MonoBehaviour { Editor, such as:

public AudioSource backgroundMusic;


// Use this for initialization
void Start () {
Get the component through a reference to a gameObect, such as:

} gameObject.GetComponent<AudioSource>();

// Update is called once per frame Instantiating Prefabs


void Update () {
You can dynamically create, or instantiate gameObjects in a
} scene from other gameObjects or Prefabs:
}
GameObject spawnedObject = Instantiate
Important concepts in Unity (prefabObject, spawnPosition, spawnRotation) as
GameObject;
• MonoBehavior is the base class that all Unity scripts are
derived from.
• Public variables are exposed in the editor Common Game Events
• Public variables and functions can be accessed when you Awake & Start - called once. This is where you can set things up.
have a reference to the component
• You can make an Object global by creating a public static Update - is called every game cycle before rendering a frame.
variable This is where most game behaviour code goes, except physics
code.

Referencing GameObjects
FixedUpdate - is called once every physics time step. This is the
place to do physics-based game behaviour.
You can reference a gameObject in code in the following ways: 

OnCollisionEnter is called when this collider/rigidbody has begun
If the script is attached to the gameObject, you can reference the touching another rigidbody/collider.
gameObject as: 

OnTriggerEnter is called when this collider has touched another
this.gameObject or simply gameObject collider that is tagged as a trigger.

Useful API Classes


If we want to reference a gameObject that the script is NOT
GameObject, Time, Transform, RigidBody, AudioSource
attached to, we can do this in a number of ways:
(look in documentation for details and other UnityEngine classes)
We can have a public variable of type GameObject that we attach
to another gameObject or prefab in the Unity Editor.

public GameObject target;

We can search for the game Object using GameObject.Find to


search for a GameObject by name:

GameObject target = GameObject.Find(“Enemy”);

We can search for the game Object using


GameObject.FindWithTag to search for a GameObject with a
particular tag:

GameObject target = GameObject.FindWithTag(“Player”);

2 of 2

You might also like