- Home /
Animation.CrossFade help please
I want to make this line of the script work, but it gives me an error. This is the part of the script
animation.CrossFade( Idle.name, 0.15f);
do I need to make a variable or what?
What is the error? Does Idle.name have a value? If so, does that property/field of Idle have a proper value that is an animation name on the model?
1 The name `Idle' does not exist in the current context
2 The best overloaded method match for `UnityEngine.Animation.CrossFade(string, float)' has some invalid arguments
3 Argument `#1' cannot convert `object' expression to type `string'
These are the errors. I'm not good at scripting so I don't know what do you mean in your comment! :)
Answer by Montraydavis · Nov 08, 2012 at 06:56 PM
Idle.name seems to be invalid for this circumstance.
Is your animations name "Idle.name" ?
Go to your Project folder, find the animation, and click it. In your inspector, look for the name, and use
animation.CrossFade ( "Idle", 1.5f ) ; // Change Idle to whatever the name you got earlier.
Answer by JustinRyder · Nov 08, 2012 at 07:00 PM
CrossFade(string, float) takes a string for the first parameter that is the name of the AnimationClip to fade to.
Assuming Idle references an AnimationClip on the Animation, then it should properly fade to it with your code. I would try hard coding the name of the clip first.
animation.CrossFade("Idle");
If that doesn't work, then make sure that the script this is in is a component of the same GameObject that the Animation is a component of. If the Animation is a component of a parent or child of this script's GameObject, the animation reference could be null. If there are multiple Animations on the same GameObject, the animation reference could point to a different one.
Answer by Landern · Nov 08, 2012 at 06:57 PM
The second parameter or argument of the CrossFade method is correct. The first argument/parameter needs to be the name of the animation, example:
animation.CrossFade("Run", 0.15f);
or
animation.CrossFade("Idle", 0.15f);
The animation needs to exist, rather be imported with the model, you call that by it's name in the first argument either as a literal string or a property that contains the name as a string.