Increase animation speed of animator

alright so i have a game object in my scene that has a sprite renderer, an animator, and a single script. If I just play the animation it looks fine but I need to increase its playback speed. The script looks like this…

using UnityEngine;
using System.Collections;

public class NEWExplosionAnimation : MonoBehaviour 
{

	void Start () 
	{
		animation["explosion"].speed = 1.5f;
	}
}

Right now there doesnt seem to be any difference with or without the script, except that I do get an error. The error i get is “MissingComponentException: There is no ‘Animation’ attached to the “explosion” game object, but a script is trying to access it.”.

So my question is how do I increase the speed that this animation is going to play, given that I have an animator and apparently I don’t have an animation. Do I need to add an animation component to this game object? or do I need to make another game object entirely for my animation?

If anyone can point me to a helpful tutorial about sprite-sheet animation (instead of unity’s 3d model statemachine heavy tutorial) that would also be fantastic.

2 Answers

2

If you have an Animator attached to the object, you should be able to affect the overall playback speed:

Animator animator = GetComponent<Animator>();
animator.speed = 1.5f;

thank you so much, that was way more frustrating than i thought it was going to be, and the solution was satisfyingly simple

If you want to call an animation, as in your example, you have to have an Animation component with the animation clips added.

so I've since added an animation component with 'explosion' as the animation. At this point I get a new error "NullReferenceException: GetRef" which points to my single line of code up there. I also have a warning "The AnimationClip 'explosion' used by the Animation component 'explosion' must be marked as Legacy." the animation speed still seems unaffected

for clarity, this is what my inspector looks like [32839-inspecterscreenshot.jpg|32839]