- Home /
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[i], "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.
Answer by Chronos-L · Jan 20, 2013 at 05:07 AM
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)]
);
}
}
I like the update you made and I have added it to my script.
Here's the full error message:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) AttackAnim.Update () (at Assets/Scripts/AttackAnim.js:20)
The last line said "AttackAnim.Update () (at Assets/Scripts/AttackAnim.js:20)", this tell you that the problem occurred at line 20 of the AttackAnim.js, So does my solution hit the mark and solve the problem you have?
No, apparently it doesn't work. And the error message I pasted in was from after I had added your update. also, the error doesn't come until I run the game and click the mouse button.
I've come up with a better alterative, now you can chose what attack you want.
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$ouse0))
{
animation.Play("SideSwing");
CAN_TA$$anonymous$$E_D$$anonymous$$G = true;
CanTakeD$$anonymous$$G = true;
Invoke("ReAttack", 1);
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$ouse1))
{
animation.Play("LeftSideSwing");
CAN_TA$$anonymous$$E_D$$anonymous$$G = true;
CanTakeD$$anonymous$$G = true;
Invoke("ReAttack", 1);
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$ouse2))
{
animation.Play("DownSwing");
CAN_TA$$anonymous$$E_D$$anonymous$$G = true;
CanTakeD$$anonymous$$G = true;
Invoke("ReAttack", 1);
}
but, I would still like to know why my fist script doesn't work, I will need random animation in the future.
I think it is the na$$anonymous$$g of the animation clips that causes the problem.
I will suggest that you store a string array of the idle animation. Then you can access the random idle animation via animation.Play( animName[ Random.Range( 0, animName.Range ) );
Another suggestion to your latest code:
You can put the 4 lines of code in each if-statement in a function, then at you can call it as AttackWith("LeftSideSwing")
, it is much cleaner that way.
if( something ) { AttackWith("SideSwing"); }
if( something ) { AttackWith("LeftSideSwing"); }
void AttackWith( string animationName ) { animation.Play( animationName ); }
This will make your code cleaner and easier to extend and add-in new codes.
Your answer
Follow this Question
Related Questions
Multiple Animations, Please Help 1 Answer
Castlevania style stop and attack script. 0 Answers
Animator Random Parameter Set 0 Answers
Linked Attackmoves, not talking to Mecanim? 0 Answers
Making an Array of Animations Play at Random With No Repeats 3 Answers