11
USING UNITY JAVASCRIPT

USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

Embed Size (px)

Citation preview

Page 1: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

U S I N G U N I T Y

JAVASCRIPT

Page 2: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

CONVENTIONS AND SYNTAX IN JAVASCRIPT

Case SensitivityAll keywords like var or function must be in lowercase. All variable names, function names and identifiers are case sensitive. The following two variables are separate and independent of each other:

var Test = 6;

var test=4;

Test and test will not be classified as the same variable and will cause a Javascript error

WhitespaceLike most programming and scripting languages, Javascript requires one space between keywords, names and identifiers. Extra spaces are ignored so you can use tabs and spaces wherever you need to make your code easier to read and debug

Semi-colonsEvery line of Javascript must end in a semi-colon ;Blank lines don't need sem-colons.

Page 3: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

CommentsYou can add in comments to help yourself keep track of parts of script. This does not effect the script itself and is not visible

// this is a single line comment

/* this is amulti-line comment */

Identifiers (Variable names, function names, labels.)The first character in an identifier name must not be a digit.

Some key words include:

VariablesDeclare variables using the keyword var. Javascript variables have no explicit data type and can contain data of any type.

var mynumber;

Page 4: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

EXAMPLE 1- HELLO WORLD

This is a basic script. Create a new JavaScript script in Unity. Copy and paste the code and click save.

#pragma strict

function Start (){Debug.Log (“Hi my name is”);Debug.Log (“Welcome to programming”);}

Create a new empty GameObject and attach the script to it.If you get stuck get the link to Tom’s demonstration.

Page 5: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

EXAMPLE 2- MATH

#pragma strict

var num1 : int = 3; var num2 : int = 4; var num3 : int; var displaytext : String; //empty for now

function Start () {

Debug.Log("Number 1 = " +num1); Debug.Log("Number 2 = " +num2); num3 = num1 + num2; Debug.Log("Number 1 + Number 2 = " +num3); }

Page 6: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

EXAMPLE 3- CHANGE COLOUR (IF STATEMENTS)

#pragma strict  

function Start () {}

function Update(){ if(Input.GetKeyDown(KeyCode.R)) { gameObject.renderer.material.color = Color.red; } if(Input.GetKeyDown(KeyCode.G)) { gameObject.renderer.material.color = Color.green; } if(Input.GetKeyDown(KeyCode.B)){ gameObject.renderer.material.color = Color.blue; }}

Page 7: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

IF ELSE STATEMENTS

http://unity3d.com/learn/tutorials/modules/beginner/scripting/if-statements

This is more advanced, but you will get an understanding of the importance of if, else statements

Page 8: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

FOR LOOPS

FOR LOOPS through a block of code a number of times.

Format:

(set initial variable; condition; action on the variable)

(rules, action)

Page 9: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

EXAMPLE 4- LOOPING (FOR)

#pragma strict

var numEnemies : int = 3;

function Start () {

for(var i : int = 0 ; i < numEnemies ; i++) {

Debug.Log("Creating enemy number: " + i);

} }

(set initial variable; condition; action on the variable)

Page 10: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

EXAMPLE 5- ROTATE OBJECT

#pragma strict

var variableRotationRate = 0.0f; var fixedRotationRate = 0.0f;

function Update () { transform.Rotate( Vector3.up * Time.deltaTime * ( variableRotationRate * 360.0f ) ); } function FixedUpdate() { transform.Rotate( Vector3.up * Time.deltaTime * ( fixedRotationRate * 360.0f ) ); }

Page 11: USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

C# VS. JAVASCRIPT SYNTAX

If you are planning to continue on creating the game, I suggest you listen carefully at the difference between C# and JavaScript, as we are learning JavaScript yet all scripting in the game is in C#.

http://unity3d.com/learn/tutorials/modules/beginner/scripting/c-sharp-vs-javascript-syntax