- Home /
Playing animation when GameObject is destroyed
I've been working on a horror game and I've managed to create a "Key script" in order to pass through levels but I'm having a short problem by editing my script to play a animation once my "Key" is destroyed, so here's the script:
var hasKey = false;
function Update(){
if(Input.GetKeyDown(KeyCode.E)){ //If E is pressed
Debug.Log("Pressed");
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 100)) { // Sends out a raycast to check if something is in front of you
Debug.Log("hit Something " + hit.transform.name);
if(hit.transform.name.Equals("Key")){ // If the object in front of you has the tag Key
hasKey = true;
Destroy(hit.transform.root.gameObject);//Destroy Key Object
}else if(hit.transform.name.Equals("Door")){ //Checks if the gameobject you're looking at has the tag Door
if(hasKey)
Debug.Log("HasKey");
hit.transform.SendMessage("Unlock"); //Calls the function Unlock on the door
}
}
}
}
If there's more information that you guys might need, let me know in the answers below! :D
Thanks.
Answer by chelnok · Aug 20, 2013 at 07:35 PM
If the animation is inside the gameobject you are destroying, you need to play animation first, and destroy gameobject after animation is finished. Sure you have to check if animation is done. You can do that like this:
Here: http://docs.unity3d.com/Documentation/ScriptReference/Animation-isPlaying.html
Animation.isPlaying
var isPlaying: bool;
Description Are we playing any animations?
// Plays an animation only if we are not playing it already.
function OnMouseEnter() {
if (!animation.isPlaying)
animation.Play();
}
Well my animation is somewhere else, on a GUI. Is there a different way to do this?
Sure thing, many ways.. I suppose your animation is: http://docs.unity3d.com/Documentation/ScriptReference/Component-animation.html
one way: make var myAni:Transform to script you posted, drag gameobject that contains the animation, and just call myAni.animation.Play() first, and after that your destroy code.
Umm... noting seems to be happening. Should I give you my edited script? I think something is wrong :/
what kind of animation are we talking about here anyway..?
Answer by meat5000 · Aug 23, 2013 at 07:41 PM
Will this function work for you? I've not used it yet to be honest. Place your animation.play in this function and it should be performed when the object is destroyed. It cannot be used as a coroutine apparently.
i really dunno if its just me ..but i'm starting to feel dizzy. What does that have to do anything? I think i have understood the very question totally wrong :)
edit: sorry.. i read the link was $$anonymous$$onoBehaviour.Destroy (i didnt even know there was OnDestroy)
OnDestroy(). A function that executes some code after you call Destroy(). In this case put OnDestroy() in '$$anonymous$$ey' script. When its destroyed OnDestroy contents will be executed.
I'm thinking that you are the first one to scroll down this far :P Surely this is the answer :D
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Run Sprint animation and stop it when button is released 1 Answer
Animation problem in script 0 Answers
Simple animation on game object doesn't play via animation.Play 1 Answer
problem not playing animation on keyDown 2 Answers