Random attack animation

I am making a first person game with melee weapons.
I want to make a script that plays a random attack animation when I press the left mouse button. I have tried to make a script, however it’s not working. when I press the mouse button, it gives me this error: “NullReferenceException: Object reference not set to an instance of an object”

here is the script:

var allmotions : AnimationClip[];

var textFieldString = "text field";



function Start () {



    for (var i = 0; i < allmotions.Length; i++)



        animation.AddClip(allmotions*, "random"+i);*

}

function Update()

{

var randomAnim = allmotions[Random.Range (0, 0)];

  • if(Input.GetButtonDown(“Fire1”))*

  • {*

transform.identity.animation.Play (“” + randomAnim);

  • }*

}
Your help would be much appreciated.

In which line does the error occur? Unity’s console should tell you that.

By the way, in this line var randomAnim = allmotions[Random.Range (0, 0)]. I am pretty sure that you will always get allmotions[0] instead of a random animation.

And why do you have a double quote in transform.identity.animation.Play ("" + randomAnim);? I think just transform.identity.animation.Play (randomAnim); would suffice.

function Update()
{
    if(Input.GetButtonDown("Fire1"))
    {
        transform.identity.animation.Play(
            allmotions[Random.Range (0, allmotions.Length)]
        );
    }
}