Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wartorg · Jan 20, 2013 at 02:52 AM · animationjavascriptrandomattack

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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)]
         );
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wartorg · Jan 20, 2013 at 11:45 AM 0
Share

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)

avatar image Chronos-L · Jan 21, 2013 at 04:35 AM 0
Share

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?

avatar image Wartorg · Jan 21, 2013 at 12:27 PM 0
Share

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.

avatar image Wartorg · Jan 21, 2013 at 04:52 PM 0
Share

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.

avatar image Chronos-L · Jan 22, 2013 at 10:16 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges