unity3d

Get Component – Script from Another Object

Get Component – Script from Another Object

Create a new scene with:

1. Camera

2. GUI Text

3. Cube, attach ‘CubeScript.js’


#pragma strict

// private per nasconderla in Inspector: nome dello script da prelevare
private var gameController : GameControllerScript;

function Start () {
    // inserisco in una variabile l'oggetto con tag GameController
    var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController");
    // se l'oggetto con tag GameController esiste inserisco in una variabile il componente GameControllerScript.js
    if (gameControllerObject != null)
    {
        gameController = gameControllerObject.GetComponent (GameControllerScript);
    }
    // se l'oggetto con tag GameController non esiste restituisce un messaggio di errore
    if (gameControllerObject == null)
    {
        Debug.Log ("Cannot find 'GameControllerScript.js' script");
    }
}

function Update () {
}


function OnMouseDown ()
{
        // When you click over the object
        Debug.Log('clicked'); // Debug Code, remove in the end
        // invia a GameControllerScript.js, alla funzione TextUpdate() la variabile myNewTextValue
        var myNewTextValue : int = 2;
        gameController.TextUpdate (myNewTextValue);
}

4. Empty Object:

a. name it ‘GameController’
b. tag it ‘GameController’
c. Transform> Reset, it resets the position at 0,0,0
d. attach ‘GameControllerScript.js’


#pragma strict

var mytext : GUIText; // Assign it in Inspector
private var myTextValue : int; // hide it in Inspector, we want drive it only via code!

function Start () {
// give them an initial value when the game starts
mytext.text = "First text"; 
myTextValue = 1;
}

function Update () {
}


function TextUpdate (myNewTextValue : int) {
if (myNewTextValue == 2)
    {
        // play the track 1, volume 0-1
        mytext.text = "Second text";
    }
    // if (trackValue == 2) -> it will play track 2 and so on...
}

e. Inspector> ‘GameControllerScript.js’ assign the GUIText into var slot

5. Play, when you click over the Cube the GuiText changes!

For italian people: Come funziona?

1. ‘CubeScript.js’ controlla l’esistenza di GameController(oggetto)
2. se esiste ottiene da GameController(oggetto) il componente ‘GameControllerScript.js’
3. invia al click del mouse a GameControllerScript.js> funzione TextUpdate() un valore
4. ‘GameControllerScript.js’ riceve questo valore e cambia la stringa di testo.

By |Unity3D, Video Games Development|Commenti disabilitati su Get Component – Script from Another Object

Unity 3D Game Engine – Materials – Fade Between 2 Materials

Unity 3D Game Engine – Materials – Switch Materials

1. Project> RMB> Create> Material> Create: material1 and material2

2. MAIN TOP MENU> Gameobject> Create Other> Cube, assign the script:

SwitchMaterial.JS


#pragma strict

	var material1 : Material; // assign in Inspector
	var material2 : Material; // assign in Inspector
	var transitionTime = 2.0;

	function Start () {
	}

	function Update () {
	ChangeMaterial();
	
	}
	
	function ChangeMaterial () {
		renderer.material = material1;
		yield WaitForSeconds (transitionTime); // wait seconds... -> non può essere inserito all'interno di Update()
		renderer.material = material2;         // switch to material2
		
	}

3. Project DRAG AND DROP material1 and material2 over Inspector> SwitchMaterial.JS> material1 slot | material2 slot

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Materials – Fade Between 2 Materials

Unity 3D Game Engine – Set Material Transparency

Unity 3D Game Engine – Set Material Transparency

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Transparent/Diffuse

NOTICE; YOu HAVE TO USE a Transparent shader, in Unity3D only Transparent Shaders support alpha channel

3. Select the Cube and assign SetTransparency.JS:


#pragma strict

	function Start () {
	}

	function Update () {
		gameObject.renderer.material.color.a = 0.5; // set transparency to 50%
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Set Material Transparency

Unity 3D Game Engine – Fade Out Transparency

Unity 3D Game Engine – Fade Out Transparency

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Transparent/Diffuse

NOTICE; YOu HAVE TO USE a Transparent shader, in Unity3D only Transparent Shaders support alpha channel

3. Select the Cube and assign FadeOutTransparency.JS:


#pragma strict

	// Fades from minimum to maximum in seconds
	var minimum = 1.0;
	var maximum = 0.0;
	var fadeoutSpeed = 1f; // 0.1 slow, 2 fast

	function Start () {
	}

	function Update () {	
		gameObject.renderer.material.color.a = Mathf.Lerp (minimum, maximum, fadeoutSpeed * Time.time);
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Fade Out Transparency

Unity 3D Game Engine – Materials – Fade Between 2 Base Colors

Unity 3D Game Engine – Materials – Fade Between 2 Base Colors

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Diffuse

NOTICE: the script will fade between ‘Shader> ‘Main Color’ values, you HAVE TO REMOVE all textures if you want to see ‘Main Color’!

3. Add to the Cube Fade.JS


#pragma strict

	// Fade the color from red to green
	// back and forth over the defined duration
	var colorStart : Color = Color.red;
	var colorEnd : Color = Color.green;
	var duration : float = 1.0;

function Start () {

}


	function Update () {
		var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
		renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
	}

4. Done!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Materials – Fade Between 2 Base Colors