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 TechedEd · Jan 12, 2014 at 04:14 PM · animationplaykeypresscharacter animation

How to play animation on keypress

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);
     }
 }
Comment
Add comment · Show 8
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 sanmn19 · Jan 12, 2014 at 04:27 PM 0
Share

Have you attached an animation component to your gameObject?

avatar image semiessessi · Jan 12, 2014 at 04:30 PM 0
Share

it gave you a call stack with that presumably? check if animation is null - otherwise somthing else is wrong - perhaps the Dance animation is missing? also, why is there a variable 'dance' here? you are not using it in the code you provide...

avatar image TechedEd · Jan 12, 2014 at 04:50 PM 0
Share

Yes, I've attached the animation to my character.

avatar image TechedEd · Jan 12, 2014 at 04:52 PM 0
Share

Yes, it gave me a call stack, I have the dance animation in my project and scene, and I added the variable dance just incase I wanted to change the animation in project view ins$$anonymous$$d of going into the script.

avatar image semiessessi · Jan 12, 2014 at 04:57 PM 0
Share

what was the call stack?

this is often useful information. sorry but i don't trust you to have not included it with good reason - i have no idea what your thought process is.

the fact that a keypress is involved here is completely unimportant so I also have a measurable bit of evidence in the question title that suggests I can not trust that you did not include it with good reason.

there is a small probability that someone can tell you precisely the problem simply by inspecting the call stack.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by thornekey · Jan 12, 2014 at 04:35 PM

give your player the animation component. and make sure the animation file is called "Dance"

Comment
Add comment · 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
0

Answer by semiessessi · Jan 12, 2014 at 06:56 PM

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)

Comment
Add comment · 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

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Playing Animation When Two Keys Are Held Down To Play Run Animations 0 Answers

How to make player attack animation play through without holding down a key. 0 Answers

Help With Animation Play Back - Reverse 1 Answer

Animation will only play once with mouse click and key press, i want it to play anytime these are pressed together.,animation will only play once, but wont play at all if another animation plays first. 0 Answers

Playing animation when GameObject is destroyed 2 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