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

Unity3D – AddComponent – Javascript

Unity3D – AddComponent – Javascript

AddComponent to THIS GameObject

Create a Cube and attach:


#pragma strict

function Start() {
        // Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = gameObject.AddComponent ("SphereCollider");
	}
	
function Update(){
	}

Play and watch Cube params into Inspector

AddComponent to ANOTHER GameObject

Create a Cube

Create an EmptyObject and attach:


#pragma strict

private var cubeobj : GameObject;

function Start() {
	cubeobj = GameObject.Find("/Cube"); // Find gameobject with name

        // Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = cubeobj.AddComponent ("SphereCollider");
	}
	
function Update(){
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – AddComponent – Javascript

Unity3D – Position of a GameObject – JavaScript

Unity3D – Position of a GameObject – JavaScript


        // Move the object to (0, 0, 0)
	transform.position = Vector3(0, 0, 0);
		
	// Print the x component of the position to the Console
	print(transform.position.x);

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Position of a GameObject – JavaScript

Unity3D – Game Engine – Get Material Name

Unity3D – Game Engine – Get Material Name

Get the material of THIS gameobject.
IMPORTANT: material is not a Component but a class of renderer Component, the correct statement is:

...
// GameObject.Component.Class.Property
this.renderer.material.name
...

Create a Cube, assign a material named ‘material0’, assign the script:


#pragma strict

function Start () {

}

function OnMouseDown ()
{
        // When you click over the object play audio
        audio.Play();
        
        // Check material
        // NB: il materiale non è un componente ma una classe di renderer!!!!
        var myMaterial = renderer.material;
        Debug.Log(myMaterial); // -> Risulta: material0 (Instance)
        
    if (this.renderer.material.name == "material0 (Instance)") {
        Debug.Log("material0 found!");
    } else {
        Debug.Log("material0 not found!");
    }
}

function Update () {

}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Game Engine – Get Material Name