Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,"— Presentation transcript:

1 USING UNITY JAVASCRIPT

2 CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All 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 Whitespace Like 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-colons Every line of Javascript must end in a semi-colon ; Blank lines don't need sem-colons.

3 Comments You 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 a multi-line comment */ Identifiers (Variable names, function names, labels.) The first character in an identifier name must not be a digit. Some key words include: Variables Declare variables using the keyword var. Javascript variables have no explicit data type and can contain data of any type. var mynumber;

4 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.

5 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); }

6 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; }

7 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

8 FOR LOOPS FOR LOOPS through a block of code a number of times. Format: (set initial variable; condition; action on the variable) (rules, action)

9 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)

10 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 ) ); }

11 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


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

Similar presentations


Ads by Google