- Home /
How to make a animation stop looping.,How to destroy animation after played.
In my game I have dynamite and when it touches an object it is supposed to disappear and play the explosion animation once but when it touches the object the animation keeps on looping. Any tips to make it play only one time? I will reward anyone who tells me how to play the animation once with one reputation point =). Thanks!
Answer by I_Am_Err00r · Jul 12, 2019 at 04:02 PM
Let me help you out.
If I understand you want the animation to play when something enters the collider and then stop playing after one loop then destroy the object? I'm going to go with your example and see you call this in start; there are two ways to do this, I'll show you the Invoke() method.
For this to work, first setup a public float called time, or just define the time in the code where I wrote time:
private void Start()
{
Invoke("Stop", time);
}
then do something like this:
private void Stop()
{
Stop = true;
//I would have a reference to the animator component here to set that bool to stop, I don't see it in your script but mine would look like this: animator.SetBool("Stop" Stop);
Destroy(this.gameObject);
}
I'm not sure how you're animations are triggered, the example you showed just has it in Start, so I hope this is what you're trying to achieve.
Shows me a bunch of errors but do you know how to delay time?
Sorry I meant do you know how to delay time another way?
There are many other ways, but I won't share that with you because the Inovke() method is the easiest way and if you set it up the way I explained I'm 1000% confident it won't give you any errors.
Tell me what the errors are, or better yet can you just paste the actual script you are using and I will make it work.
Found out why it did not work! $$anonymous$$y computer was glitching badly and did not respond to looping off. Now it works! Heres a point for helping me
I saw in my emails that you did accept this answer, but it seems to have reverted back; it would mean a lot if you would try again and make sure it highlights the answer as green. Thanks @zacharif!
Answer by I_Am_Err00r · Jul 11, 2019 at 05:19 PM
On the actual animation file itself, by default unity always sets "Looping" to true.
Find the animation file you want, uncheck the looping box, done.
Answer by zacharif · Jul 12, 2019 at 03:08 PM
@skatesa207 Thanks for the help but I already tried that. It did not work =/.
I almost got it! For all these people following please tweak it and comment if it works.
On collision or whatever triggers your animation. Write Destroy(this.gameObject); Send$$anonymous$$essage("Stop");
Your object will disappear and send a message, then on the animation write a script.
// Start is called before the first frame update void Start() { bool Stop = true;
Destroy(this.gameObject);
}
That means once the message is sent it will disappear, I just need to delay it to play the animation and then disappear. If anyone knows how to delay please comment.
Thanks!
Your answer
Follow this Question
Related Questions
Repeat loop every 5 secounds 1 Answer
Animations in the animator not looping 1 Answer
I can't put animations on loop. 1 Answer
button animation loop unity 2D 0 Answers