- Home /
2D Animation Help, noob question
Ok I Started today to learn 2D animations and what I did was importing a sprite that I later sliced to make the frames of an explosion, I created and adjusted a 2D animation out of it and if I put the object in the scene as soon as the game start said animation loops continuosly and everybody is happy and lives free. But as soon as I set the gameobject animation not to loop after it has finished, and try to code that after it finishes said gameobject needs to be destroyed (no need to have the final frame of an explosion on the screen) I get an exception : "There is no 'Animation' attached to the "DefExplosion" game object, but a script is trying to access it."
Here's what I've tried so far :
public class ExplosionEnd : MonoBehaviour {
Animation Anim;
// Use this for initialization
void Start ()
{
Anim = GetComponent<Animation>();
gameObject.GetComponent<Animator>();
gameObject.GetComponent<Animation>();
}
// Update is called once per frame
void Update ()
{
if (!gameObject.animation.isPlaying)
{ Destroy(gameObject); Debug.Log("Destroyed"); }
}
the above script is attached to the gameobject that has the animation and need to be destroyed afterwards.. what am I missing here ?
Answer by Kamuiyshirou · Apr 30, 2014 at 05:27 PM
Hi Dude LIKE MY answers!
1 - Create this CODE in C# and drag in your object:
using UnityEngine;
using System.Collections;
public class DestroyAnimatio : MonoBehaviour {
void DestroyAfeteranimation() {
Destroy(gameObject);
}
}
2 - Open in : Window > Animation
3 - Create your animation and determine the time.
4 - Still in the Animation window, click the mouse exactly in the range of destruction of the object. With setada position on the chart, add an event by clicking ADD EVENT ', near ADD FRAME.
5 - Will open a small screen called "Edit Event Animation". Select the function created in the above code.
6 - READY! Start the game NOW!
Hope that helps you!
TIP A FRIEND FOR SELECT LOOP ANIMATION >
(http://files.photosnack.net/albums/images/d3d4f1d5abe364aa2ccacdi212747256/scale-1000x1000)
Dude just by unchecking that checkbox the object wont disappear from the scene. The last played frame will stay there unchanged
Hi Dude LI$$anonymous$$E $$anonymous$$Y answers!
1 - Create this CODE in C# and drag in your object:
using UnityEngine;
using System.Collections;
public class DestroyAnimatio : $$anonymous$$onoBehaviour {
void DestroyAfeteranimation() {
Destroy(gameObject);
}
}
2 - Open in : Window > Animation
3 - Create your animation and deter$$anonymous$$e the time.
4 - Still in the Animation window, click the mouse exactly in the range of destruction of the object. With setada position on the chart, add an event by clicking ADD EVENT ', near ADD FRA$$anonymous$$$$anonymous$$
5 - Will open a small screen called "Edit Event Animation". Select the function created in the above code.
6 - READY! Start the game NOW!
Hope that helps you!
Answer by coolbird22 · Apr 30, 2014 at 04:17 PM
Did you create the animation clip by selecting the character game object and then create it ? I did not and it created problems for me just yesterday.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Animator.GotoState: State could not be found. 2 Answers
Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer
How to check in code when an animation is over? 1 Answer
Efficient way to instantiate a sprite that plays an animation then destroys itself? (3d space) 1 Answer