x


appear/disappear gameobject

hi, i've created several gameobjects in my scene. now at runtime i want only a few to be shown at startup. later i'd like to have some gameobjects fade away and completely disappear, while others to appear fading in. how do i do that in a generic way, i. e. a function taking the object id as param? how do i tween material alpha using Ani.Mate generically? thanks, flexrails

more ▼

asked Dec 04 '09 at 08:53 AM

flexrails gravatar image

flexrails
706 29 35 55

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

To make them appear and dissappear (without fading) is very simple. Just enable or disable each game object's renderer component. Here's the example from the docs:

// make the object invisible
renderer.enabled = false;

// make the object visible
renderer.enabled = true;

// toggle object's visibility each second
function Update () {

    // Find out whether current second is odd or even
    var seconds : int = Time.time;
    var oddeven = (seconds % 2) == 0;

    // Enable renderer accordingly
    renderer.enabled = oddeven;
}

For fading individual game objects in and out, you'll need to use one of the shaders from the "Transparent" family, then adjust the alpha value of the main colour, for each gameobject's material.

Note that when you modify a game object's material using Renderer.material, you actually create a temporary clone of that material so that it is no longer shared with other gameobjects. This means you'll be able to fade gameobjects individually even though they may share the same material in the editor. Any clones that are made at runtime like this are cleaned up and re-set when you click stop.

more ▼

answered Dec 04 '09 at 10:00 AM

duck gravatar image

duck ♦♦
41k 92 148 415

(comments are locked)
10|3000 characters needed characters left

that's what i ended up with for tweening alpha material using AniMate. hope it helps.

function TweenToAlpha(object:GameObject, duration:float, toAlpha:float) {
    var renderers = object.GetComponentsInChildren(Renderer);
    var materials:Array = [];
    for (var r in renderers) {
    	for (var m in r.materials) {
    		materials.Push(m);
    	}
    }

    for (var m in materials) {
    	Ani.Mate.To(m, duration, {"colorName": "_Color", "a": toAlpha});	
    }
    yield WaitForSeconds(duration);
}
more ▼

answered Dec 04 '09 at 01:00 PM

flexrails gravatar image

flexrails
706 29 35 55

(comments are locked)
10|3000 characters needed characters left

For switching between multiple game objects look also at the answers provided to this question: http://answers.unity3d.com/questions/1124/how-to-change-my-character-during-the-game/

more ▼

answered Dec 04 '09 at 10:19 AM

elevishstar gravatar image

elevishstar
59 1 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2087
x814
x275
x245
x64

asked: Dec 04 '09 at 08:53 AM

Seen: 11722 times

Last Updated: Mar 08 '12 at 08:40 PM