I gave this script to my player, but when I press the button it says NullReferenceException
var dance : AnimationClip;
function Update ()
{
if(Input.GetKeyDown(KeyCode.B))
{
animation.Play("Dance", PlayMode.StopAll);
}
}
I gave this script to my player, but when I press the button it says NullReferenceException
var dance : AnimationClip;
function Update ()
{
if(Input.GetKeyDown(KeyCode.B))
{
animation.Play("Dance", PlayMode.StopAll);
}
}
give your player the animation component. and make sure the animation file is called “Dance”
Assuming that the file this code is in is called ‘Player Functions.js’ then the problem is that animation is null when the code is being called.
Wherever you are initialising it with ‘GetComponent’ is either failing because the component is not attached or there is no such call being made. Whatever the case the ‘animation’ variable is not being initialised correctly or at the right time. The solution would be something to ensure that there is something like this in your class definition:
var animation : Animation;
and something like this in your Awake or Start function:
animation = gameObject.GetComponent( Animation );
Its a shame that the Unity documents do not include the required boilerplate in their examples - if you follow them naively then you would try and use animation without ever declaring it or initialising it, which I suspect is what you have done. (for example see the documentation for Animation.Play)