Hello Guest

Author Topic: Making the game wait for animation to finish?  (Read 6312 times)

Teboto

  • Newbie
  • *
  • Posts: 9
    • View Profile
Making the game wait for animation to finish?
« on: May 22, 2013, 03:41:47 am »
Hi, I have a function in which I'd like to have an animation play, destroy the animated sprite when it finishes, and then execute the rest of the logic in my function (all of this being visible to the player). Is this possible? If so, how would I go about doing this? Also, can you briefly explain what's going on behind the scenes when an animation is performed? Does an animation secretly use the game's update loop? Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Making the game wait for animation to finish?
« Reply #1 on: May 22, 2013, 12:28:17 pm »
There is no "game update loop" in Unity. Each behaviour has Update, LateUpdate, etc. functions that get called at the correct time. Refer to the linked docs -
http://docs.unity3d.com/Documentation/ScriptReference/

To answer your question, what you can do to achieve this is:
1. Create a MonoBehaviour, call it destroy on complete or something
2. In Start, do this:
Code: [Select]
   tk2dAnimatedSprite sprite = gameObject.GetComponent<tk2dAnimatedSprite>();
   sprite.animationCompleteDelegate = delegate(tk2dAnimatedSprite sprite, int clipId) {
        gameObject.Destroy( gameObject );
   };
3. Attach this behaviour to your animated sprite, and it'll destroy itself when it completes playing.

The reason this isn't part of the core behaviour is that for best performance, you should really be pooling your sprites, and not destroying them every time a clip stops playing...

estudio3d

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Making the game wait for animation to finish?
« Reply #2 on: March 18, 2014, 11:15:42 pm »
Hello. I want to do that when one finishes playing animated script, destroyed. That code did not work. I get the following error:

Assets/Destroy2DTKAnimScript.cs(9,100): error CS0136: A local variable named `sprite' cannot be declared in this scope because it would give a different meaning to `sprite', which is already used in a `parent or current' scope to denote something else


What's the problem?

Thank you.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Making the game wait for animation to finish?
« Reply #3 on: March 19, 2014, 10:18:33 am »
It means exactly what that message says - there is something else called sprite in the same scope, possibly same function.