- Home /
Play animation before death...c#
Hi there,
I'm trying to activate an animation which is associated with an object. When the Object hits the collider 'Destroy', the object is killed fine, but the animation doesn't play before this. Can you help to work out how this can happen. I have the animations in the object set up and is hitting an object with a trigger selected.
void OnTriggerEnter(Collider other) {
if (other.tag == "Destroy")
{
_GameManager.GetComponent<GameManager>().Death();
Destroy(GameObject.FindWithTag("JackSteelBody"),4);
Jack.GetComponent().Play ("JackBreak");
}
The error that comes up is:
Assets/Script/Player/PlayerControl.cs(81,25): error CS0103: The name `Jack' does not exist in the current context
Many thanks for any help.
Rich
Answer by GluedBrain · Apr 15, 2013 at 08:40 PM
Well, since you're destroying the gameobject first and then playing the animation i.e.
Jack.GetComponent().Play ("JackBreak");
comes after it has been destroyed. SO there's no JACK anymore. Try attaching the animation to a different gameobject of the same kind which will be instantiated after the JACK dies.
Thanks. That works. I did think though that I put a wait on the Destroy for 4 seconds. Would that not do it?
If you do it in a coroutine it will, but since it's just a function it waits until it's finished before going to the next line. I also suggest ins$$anonymous$$d of using a fixed delay, have your coroutine wait the “.Length” of the animation so if it gets changed later you won't have to edit the code.
I have been down a long and dark road and have come out the other side to find a few other challenges. I am now now watching The Lord Of The Rings which journey I am beginning to relate to. Thanks for your help. It went some way, but only part of the puzzle. As with all these projects there are many pieces to place.
S$$anonymous$$ling the words: Hobbits! [cheers abound.] "I don't know half of you half as well as I should like, and I like less than half of you half as well as you deserve."
Rich