• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RichCoggin · Apr 15, 2013 at 07:25 PM · animationcolliderdestroy

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RichCoggin · Apr 15, 2013 at 09:19 PM 0
Share

Thanks. That works. I did think though that I put a wait on the Destroy for 4 seconds. Would that not do it?

avatar image FWCorey · Apr 15, 2013 at 10:13 PM 0
Share

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.

avatar image GluedBrain · Apr 18, 2013 at 04:47 PM 0
Share

I'm late but, did that clear your doubt?

avatar image RichCoggin · Apr 18, 2013 at 09:35 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy trigger so animation only plays once. 1 Answer

Trigger Animation doesn't work, please help me. 1 Answer

Destroying object when player walks over it 1 Answer

Destroying an object using the script of another one. 1 Answer

Can I make animations snap to a frame? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges