Sie sind auf Seite 1von 4

Classes

Classes categorize variables and functions, similarly to how you might organise pictures in categorised
folders.
Variables and function in classes are placed within the classes brackets:
public class Modifiers
{
public bool leftAlt;
}

If they do not have a class, they are declared within the class of the script itself (see below)

Syntax:
Time.deltaTime
|
|
Class Name of variable in that class
Class of the script:
The name of the script must match the name of the class stated at the start of a new script (new scripts
are automatically called NewBehaviourScript). When the name of the script is changed, the class name
changes automatically. Shown below:
public class NewBehaviourScript : MonoBehaviour {
}
Everything in the script MUST take place within the brackets placed after the class of the script and
before the end of the script.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Variables
Variables are used for storing information that can be shared between scripts and parts of scripts.

1. float speed = 9.0f


|
Variable type:
Type of value contained by variable:
String = Text and numbers stored in quotation marks
string speed = x95tgrf
int = Whole number, no decimal
int speed = 9

float = Number with decimal. f must be placed after it


float speed = 9.0f
bool = true / false
bool turbo = true
Vector3 = 3 XYZ coordinate values
Rigidbody =

2. float speed = 9.0f


|
Variable name:
Cannot conflict with other words in code, begins with letter and contains only letters, numbers, and
underscores

3. float speed = 9.0f


|
Variable value:
Can only consist of data specified by variable type, (in this case, a number with a decimal)

4. Declaring and setting variables: float speed = 9.0f;


Declaring variable: float speed;
5. Important variables to know:
Time.deltaTime - Time in seconds it took to complete the last frame. (Seconds per frame.)
VERY important to use this in the Update () function when dealing with things like movement, so you get
something to move 10 units every second, not every frame. To do this, multiply the value you want to change by
Time.deltaTime. This will cause shorter frames to have values to be multiplied by a lower amount of seconds per
frame, so they have less effect and vice versa; so you can do things based on time. Shown below.
void Update() {
float translation = Time.deltaTime * 10;
transform.Translate(0, 0, translation);
}
Moves in the Z axis 10 units each second, as opposed to each frame as would have happened without multiplying 10
by Time.deltaTime.

transform.position - World position in space

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Functions
Functions (also called methods) are used for carrying out actions and making things happen in general

1. Dog.fetchStick();
|
Function class and function name:
The name of the function (after .) and class it belongs in (before .)

2. Dog.fetchStick(25);
|
Argument:
Modifies the function (for example, fetch the stick with the speed 25.) Variables can also be placed here.

3. void Update();
|
Return type:
The type of data the function will result in:
void= Function will not receive data, only send it (most common)

4. Brackets:
Dog.fetchStick(){
}
Place whatever action the function carries out between the two brackets. For example:

void Update() {
float translation = Time.deltaTime * 10;
transform.Translate(0, 0, translation);
}

5. Basic functions:
Start ():Is called (activated) when scene first launches, for example, to set a score variable to
0 when the game begins.
Update (): Is called every frame. Crucial for checking for various variables and conditions in
game that need to be constantly checked upon.

Instantiate (): Spawns a prefab. Requires 3 pieces of information in parentheses, (what to make,
where to make it, rotation to give it)
if () {}: Checks for condition inside parentheses to be true, if it is, will carry out functions in brackets.
Rigidbody.AddForce () Applies force to object, expresed in parentheses as Vector3.
Input.GetAxis (): Returns value of axis specified in parentheses
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Comments
Comments are useful for writing down what a part of a script does, as the script will ignore
everything denoted as a comment but you will still be able to see it.
Think of annotation in Ruggis class, when you write an annotation it doesnt affect the actual
text, but you still see it and it tells you useful information.
Use // to denote a single line comment.
Start with /* to start a multi-line comment, end it with */
For example:

//This script makes the object move 10 units each second


void Update() {
float translation = Time.deltaTime * 10;
transform.Translate(0, 0, translation);
}

Das könnte Ihnen auch gefallen