Unity3D – Generic List – JavaScript

Unity3D – Generic List – JavaScript

The Generic List (also known as List) is similar to the JS Array and the ArrayList, the significant difference with the Generic List, is that you explicitly specify the type to be used when you declare it – in this case, the type of object that the List will contain.

Once you’ve declared it, you can only add objects of the correct type – and because of this restriction, you get two significant benefits:
– no need to do any type casting of the values when you come to retrieve them.
– performs significantly faster than ArrayList (5x faster)

Sample code:


#pragma strict

// you need to add a line at the top of any script in which you want to use Generic List
import System.Collections.Generic;

var someNumbers = new List.<int>();            // a real-world example of declaring a List of 'ints'
var enemies = new List.<GameObject>();         // a real-world example of declaring a List of 'GameObjects'
var vtcs = new List.<Vector3>(mesh.vertices);  // convert an array to a list

function Start(){

	myList.Add(theItem);                   // add an item to the end of the List
	myList[i] = newItem;                   // change the value in the List at position i
	var thisItem = List[i];                // retrieve the item at position i
	myList.RemoveAt(i);                    // remove the item from position i
	
	numList = nums.ToList();               // convert an array to list
	
}// END Start()

function Update(){
}// END Update()

Original article: http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Generic List – JavaScript

Unity3D – warning Messages – BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’

Unity3D – warning Messages – BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’

Implicit Downcast from… to…

BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’

This script will generate the BCW0028 error:

...

// buttons[i] -> this is an Array of GameObject

...
// Assegno Audio INIZIO #############################################################################
function AssignAudio () {
  
	var audioSrc : AudioSource;
	
	for(var i : int = 0; i < buttons.Length; i++) // assegno a tutti gli oggetti nell'array l'audio
	    {
	      	// BCW0028: WARNING: Implicit downcast from 'UnityEngine.Component' to 'UnityEngine.AudioSource'.
	      	audioSrc = buttons[i].AddComponent ("AudioSource"); 
	      	

	      	
			buttons[i].audio.clip = buttonAudio0; // Add audio clip
			buttons[i].audio.playOnAwake = false; // not play on awake  
	    }

}// END AssignAudio()
// Assegno Audio FINE ###############################################################################
...

Solution: you should explicity cast the Components to AudioSource yourself using “as” like this:

Change the row:

audioSrc = buttons[i].AddComponent ("AudioSource");

with

audioSrc = buttons[i].AddComponent ("AudioSource") as AudioSource;
By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – warning Messages – BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’

Unity3D – JS Advanced – extends System.Object

Unity3D – JS Advanced – extends System.Object

System.Object is the class from which (generally speaking) everything in Unity is derived.
Every single other thing we build in Unity, derives from System.Object.
You can use this to display variables in the inspector similar to how a Vector3 shows up in the inspector.

Attach to an Empty Object this:


class Test extends System.Object {
	var p = 5;
	var c = Color.white;
}
var test = Test ();

Inspect the Inspector!
Click the white triangle on the left of ‘Test’ to espand and see P and C var.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – JS Advanced – extends System.Object

Unity3D – Create a Class – Easy Example

Unity3D – Create a Class – Easy Example

Watch the code:


#pragma strict

class Audio // create a Class, you will have the tringle to expand Class content into Inspector
{
	var crunchSFX : AudioClip; // Assign in Inspector
}
var myAudioClass : Audio; // store the whole Class content inside a variable

 
function OnTriggerEnter(other : Collider) 
{
    // play the SFX calling Class
    audio.PlayOneShot(myAudioClass.crunchSFX, 1);
    
} // END OnTriggerEnter

The steps are:
1. Create a Class

class Audio 
{
	var crunchSFX : AudioClip; // Assign in Inspector
}

2. Store the whole Class content inside a variable

var myAudioClass : Audio;

3. Call the Class content using the statement:

//  variable with Class content . variable name
... myAudioClass.crunchSFX ...
By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Create a Class – Easy Example

Unity3D – GET variables from script attached to ANOTHER GameObject

Unity3D – GET variables from script attached to ANOTHER GameObject

Hierarchy:

– GameController (Empty GameObject) -> attach GameControllerScript.JS

GameControllerScript.js

#pragma strict

var score : int = 3; // NO PRIVATE or Cube.js can't access!!!

– Cube -> attach CubeScript.js

CubeScript.js

#pragma strict

var gameController : GameControllerScript;

function Start(){

    // get the script of GameController object START ----------------------------------
    var gameControllerObject : GameObject = GameObject.Find("/GameController");
    
     // if the object exist
     if (gameControllerObject != null) 
    {      
        // ottengo il componente script
        gameController = gameControllerObject.GetComponent (GameControllerScript);
    }
    // if the object does not exist
    if (gameControllerObject == null)
    {
        Debug.Log ("Cannot find 'GameControllerScript' script");
    }
    // get the script of GameController object END ------------------------------------

    // from the another script I will get the variable - score -
    var level : int = gameController.score; 
    Debug.Log("Level is: " + level); // the result is 3

}

For italian people: come funziona?

CubeScript.js esegue le seguenti operazioni:

1. dichiara una variabile per trovare e storare l’oggetto esterno con nome “/GameController”), notare lo slash che precede il nome necessario se l’oggetto no ha parent
– var gameControllerObject : GameObject = GameObject.Find(“/GameController”);

2. dichiara una variabile per ottenere e storare il componente dell’oggetto di cui al punto 1.
– var gameController : GameControllerScript;
– gameController = gameControllerObject.GetComponent (GameControllerScript);

3. dichiara e stora la variabile “score” proveniente dallo script esterno di cui al punto 2.
– var level : int = gameController.score;

IMPORTANTE: la variabile score DEVE essere pubblica, se è private non si potrà accedere ad essa con script esterni.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – GET variables from script attached to ANOTHER GameObject