- Home /
How to get an AnimationClip to play backwards via script?
I'm making a script for the asset store and I'm almost done, this is my last bug! Anyways it will allow players to climb up and down ladders. I'm stuck on animation though. What I'm trying to do is to play the climb animation play backwards via script so the player doesn't need to make two different animations. Here's my progress:
var climbAnimation : AnimationClip; var climbAnimationBack : AnimationClip;
function Start () { climbAnimationBack = climbAnimation; animation["climbAnimationBack"].speed = -1; }
However I get this error message: "NullReferanceExeption", and when I go back to the code the line it says I have a problem with is this part:
animation["climbAnimationBack"].speed = -1;
Is this because "climbAnimationBack" isn't an actual animation clip, just a variable? If so how do I fix it? Thanks!
P.S. I used splitting for my animations
Answer by rutter · Apr 11, 2012 at 01:44 AM
animation["climbAnimationBack"].speed = -1;
Let's step through this code:
If you're getting a null reference exception, that implies one of two things:
animation
returned null, implying there isn't an animation component attached, hereanimation["climbAnimationBack"]
returned null, implying there is an animation, but that it doesn't have a clip named "climbAnimationBack"
Now, it's important to distinguish two things:
climbAnimationBack
without quotes is a reference to anAnimationClip
"climbAnimationBack"
with quotes is a literal string
They do very different things, and don't necessarily have anything to do with each other, so it's important to keep them straight. If I had to guess, you might have meant to use climbAnimationBack.name
, which gives you a string equal to the name of that animation clip?
In any case, it looks to me like you're trying to do one of two things:
If the animation clips are already attached to your animation, you can just call them by name (this is the simplest setup).
If you're trying to add those clips to the animation, you'll need to do so before trying to access them through it.