- Home /
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
Answer by duck · Dec 04, 2009 at 10:00 AM
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.
Answer by flexrails · Dec 04, 2009 at 01:00 PM
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);
}
Answer by elevishstar · Dec 04, 2009 at 10:19 AM
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/
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Rendering an entire object as transparent 1 Answer
Change the alpha of a gameobject with multiple childs 1 Answer
Is there a shader in Unity that prevents transparent parts from overlapping? 1 Answer
Material class use general question 1 Answer