- Home /
How to make an animated object disappear?
Hello, I'm very new in Unity,just started exploring bits here and there. I'm trying to make an object that disappears after it has finished animating. Okay,here is the scenario: An inanimate object(lets say a cube) is collided with another object,it then animates in a certain way,after it finishes the animation it disappears. How to do that? I'v so far only stumble upon these renderable thingy,i think i have to make the animated cube invisible,while the station cube visible,when the visible cube is triggered or touched it becomes invisible or renderable=false and the animated cube visible(renderable=true). But after it animates how to make it disappear? Thank you for the help
Answer by aldonaletto · Feb 22, 2013 at 03:57 AM
You could deactivate the object with SetActive:
gameObject.SetActive(false);
This is better than just setting renderer.enabled to false, because all components (colliders, scripts etc.) and children of this object are deactivated as well - you'll not have problems with an invisible object haunting the scene.
First of all thanks mr Aldo :) Hm,SetActive(),cool approach,i'll try that. However that will just deactivate the animation of the object right?I want it to totally disappear after it finishes animating.Or else im gonna have to manually animate it to scale to 0,0,0 heh.
No, SetActive is a GameObject method that activates/deactivates the whole object - the object and all of its children just disappear from the scene, as if it was destroyed.
But I think you don't need inanimate and animated objects: the same object could have an animation - when colliding with something, the object could start playing its animation and deactivate itself after the appropriate delay, like this:
private var collided = false;
function OnCollisionEnter(){
if (!collided){
collided = true;
StartCoroutine(PlayAndDisappear());
}
}
// play and disappear after delay:
function PlayAndDisappear(){
animation.Play("someAnimation");
WaitForSeconds(animation["someAnimation"].length);
gameObject.SetActive(false); // deactivate object
}
NOTE: The object must be a rigidbody to generate OnCollision events when hitting another collider
Answer by OmahaNS · Feb 22, 2013 at 02:47 PM
This depends on your desired behaviour. If you want it to be invisible but still interact with your scene, set renderer.enabled to false. If you want it gone completely, call Destroy(gameObject). If you want to disable it to re-enable it later, use gameObject.SetActive(false), or if it has children, gameObject.SetActiveRecursively(false).