Unity – Scripting – Debug

Videogames Development – Unity – Debug.Log

Logs message to the Unity Console.

Statements:

// Message with a link to an object.
Debug.Log ("Hello", gameObject);

// Message using rich text.
Debug.Log("<color=red>Fatal error:</color> AssetBundle not found");

SCRIPT

MAIN TOP MENU> ASSETS> CREATE> Javascript, type the name ‘GetMousePosition’

Assets> GetMousePosition> Inspector> ‘Open…’ button

Write:

#pragma strict

function Start () {

}

function Update () {
	var mouse = Input.mousePosition;
        Debug.Log(mouse);
		
	}

Create an Object inside the Hierarchy ‘MyObject’

Assets> DRAG AND DROP ‘GetMousePosition’ script over Hierarchy> MyObject

Play> move the mouse, look at the BOTTOM OF THE SCREEN, you will see the coordinates as: (536.0, 583.0, 0.0)

By |JavaScript, Unity3D, Video Games Development|Commenti disabilitati su Unity – Scripting – Debug

Unity – Scripting – Array

Videogames Development – Unity – Array

An Array stores multiple values in a single variable.
The first array’s value is 0.

There are two types of arrays in Unity:
1- builtin arrays and normal Javascript Arrays
2- builtin arrays (native .NET arrays), are extremely fast and efficient but they can not be resized

Javascript Arrays

Assign this code to an Empty Object in the Scene:

#pragma strict

function Start()
{
    
}

function Update()
{
// Array Start
var mycars = new Array();
mycars[0] = "Saab"; // Notice: the first value is 0
mycars[1] = "Volvo";
mycars[2] = "BMW";
// Array End
 var i= 1; // Array index
 Debug.Log(mycars[i]); // The result is Volvo
}

Javascript Arrays: Print an Array Content

#pragma strict

function Start () 
{
    var strings = ["First string", "Second string", "Third string"];
    
    for(var item : String in strings)
    {
        print (item);
    }
}

Javascript Arrays: basic use

var myArray = new Array();     // declaration
myArray.Add(anItem);           // add an item to the end of the array
var thisItem = myArray[i];     // retrieve an item from position i (richiamare un contenuto dalla posizione i)
myArray.RemoveAt(i);           // removes an item from position i
var howBig = myArray.length;   // get the length of the Array

.NET Arrays

Syntax 1 – multiple lines

#pragma strict

// type [] name = new Array type[number of items]
int[] myIntArray = new Array int[3];

function Start () 
{
// name of array[index] = value
myIntArray[0] = 12;
myIntArray[1] = 16;
myIntArray[2] = 18;
}

Syntax 2 – one line

#pragma strict

// type [] name = new Array type[number of items]
int[] myIntArray = {12, 16, 18};

function Start () 
{

}

Public Array – you can see them in Inspector window

The code below creates an Array of Game Objects with Tag ‘Player’

#pragma strict

public var players : GameObject[];

function Start ()
{
    players = GameObject.FindGameObjectsWithTag("Player");
    
    for(var i : int = 0; i < players.Length; i++)
    {
        Debug.Log("Player Number "+i+" is named "+players[i].name);
    }
}
By |Unity3D|Commenti disabilitati su Unity – Scripting – Array

Unity – Scripting – Delay Execution Time

Videogames Development – Unity – Delay Execution Time

yield WaitForSeconds

#pragma strict

function Start()
{
// Prints 0
print (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5);
// Prints 5.0
print (Time.time);    
}

function Update()
{

}

NOTICE:
yield WaitForSeconds (5); -> yield (dare la precedenza) Aspetta per Secondi 5

Invoke

#pragma strict
 
function Start()
{
    // Invoke (function name, delay time in seconds)
    Invoke ("myFunction", 2);
    Invoke ("myFunctionTwo", 4);
}
    
function myFunction()
{
    Debug.Log("Delay time of 2 seconds");
}

function myFunctionTwo()
{
    Debug.Log("Delay time of 4 seconds");
}

Invoke Repeat

#pragma strict
 
var x: int;

function Start()
{
    // InvokeRepeiting (function name, delay time in seconds, repeat every x seconds...)
    InvokeRepeating("myFunction", 2, 1);
}

function myFunction()
{
    var x = x++;
    Debug.Log("Invoked " + x);
}

The result is:

Invoked 0
Invoked 1
Invoked 2
Invoked 3
… to infinity and beyond!

By |Unity3D|Commenti disabilitati su Unity – Scripting – Delay Execution Time

Unity – Scripting – Basics

Videogames Development – Unity – How to add a script

ADD SCRIPT

1. MAIN TOP MENU> Assets> Create Javascript

2. Assets> select NewBehaviourScript Import Setting

3. Inspector> ‘Open’ button, it starts MonoDevelop-Unity editor

4. Write:

function Start() 
{
    print( "Hello, world" );
}

5. MAIN TOP MENU> GameObject> Create Empty

6. Hierarchy> select ‘GameObject’ you have just created> Inspector> ‘Add Component’> Scripts> NewBehaviourScript

7. Play button> BOTTOM LEFT you will see ‘Hello, world’

BASIC STRUCTURE

#pragma strict

function Start()
{
  ... it runs one time when game start ...  
}

function Update()
{
  ... it runs every frame ...
}

NOTICE:
#pragma is a pre-compiler directive. ie it tells the compiler what to do or how to behave.
With #pragma you must tell of what type the variable will be and it will give you an error if you don’t comply.

ADVANCED STRUCTURE WITH FUNCTIONES

#pragma strict

var... declare initial value of variables -> DEFAULT ACCESS IS PUBLIC

// Single line comment

/*
Multiline comment
*/

function Start()
{
  ... it runs one time when game start ...  
}

Myfunction()
{
  ... the other funcions ...  
}


function Update()
{
  ... it runs every frame ...
Myfunction(); // call external functions
}

AWAKE – START – UPDATE

#pragma strict

function Awake ()
{
    ... setup ammo for the enemy ... 
    it starts at first, even component is not enable
}


function Start ()
{
    ... allow enemy to shoot ... 
    it starts when component is enabled
}

function Update()
{
    ... hits control ... 
    it is called once per frame to control data changes over time, use this to:
    - moving non physics objects
    - simple timers
    - receiving input
    NOTICE: update interval times vary
    RESULT SAMPLE OVER TIME: 0.01656623 - 0.01656666 - 0.01656696 (it changes)
}
function FixedUpdate()
{
    ... hits control ... 
    it is called at fixed update intervals:
    - called every physics step
    - fixed update intervals are consistent
    - regular updates: adjusting physics objects
    NOTICE: update interval times is constant
    RESULT SAMPLE OVER TIME: 0.02 - 0.02 - 0.02 (it does not change)
}
By |Unity3D|Commenti disabilitati su Unity – Scripting – Basics

Unity – Scripting – Countdown – Seconds

Videogames Development – Unity – How to create a countdown

Assign this code to an Empty Object in the Scene:

#pragma strict

var endTime : float;


function Start()
{
    endTime = Time.time + 60;
}

function Update()
{
    var timeLeft : int = endTime - Time.time;
    // We do not need negative time
    if (timeLeft < 0) timeLeft = 0;
    Debug.Log(timeLeft);
}

Take it from the top:

1. Declare ‘endTime’ variable
2. inside ‘Start()’, setup inital value ‘endTime’=’Time.time’ (this is the time in seconds since the start of the game) + 60 -> 60 seconds
3. inside ‘Update(), ‘timeLeft’ = 60 – ‘Time.time’ (this is the time in seconds since the start of the game) -> 59…58…57
if (timeLeft < 0) timeLeft = 0; we do not need negative time show inside debug console 'timeLeft'

By |Unity3D|Commenti disabilitati su Unity – Scripting – Countdown – Seconds