• 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 SirOrange · Feb 13, 2017 at 12:09 PM · animationanimatoranimate

Animation Not playing

Animation Not playing. why? error CS1061: Type UnityEngine.Component' does not contain a definition for Play' and no extension method Play' of type UnityEngine.Component' could be found. Are you missing an assembly reference?`using System.Collections; using System.Collections.Generic; using UnityEngine;

public class HeadHit : MonoBehaviour {

%|-1814071339_1|% void Start () { %|1205629864_3|% %|1624323751_4|%

%|1732494877_6|% void Update () { if (Input.GetKey(KeyCode.E)) { animation.Play ("Hit0"); %|620421206_10|% %|765253691_11|% %|-1072008687_12|% } %|1153412139_14|% }`

Comment
Add comment · Show 1
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 SirOrange · Feb 12, 2017 at 03:49 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HeadHit : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey(KeyCode.E)) {
             animation.Play ("Hit0");
         }
         if (Input.GetKey(KeyCode.Q)) {
             animation.Play ("Hit1");
         }
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by T_Lavell · Feb 13, 2017 at 07:37 PM

Try explicitly referencing your animator component. Instead of using the shortcut 'animation', try creating an Animation variable, and tell THAT to play. Try...

 public Animation _anim;
 
 void Start ()
 {
     _anim = gameObject.GetComponent<Animation>();
 }
 
 // Update... If (input ... )
     _anim.Play("AnimationNameHere");

I'm not certain this will solve your problem, but it should work. I haven't seen the specific error you've shown before, but it seems not to realize you are trying to reference the legacy animation component.

Also, unless you are working on an old project that is firmly entrenched in the legacy animation system, I would highly recommend switching to the new mecanim animator system. It requires a bit of learning to set up the animator controller transitions, but I find it is really much easier to use once you catch on to that stuff.

Comment
Add comment · Show 2 · 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 SirOrange · Feb 13, 2017 at 11:49 PM 0
Share

I have no idea how to set up an animator, do you have some kind of video/tutorial that i can learn from?

avatar image T_Lavell · Feb 14, 2017 at 05:10 AM 0
Share

https://m.youtube.com/watch?v=7RtRiniiW0c

YouTube is your friend

avatar image
0

Answer by omerd · Feb 14, 2017 at 05:52 AM

You want to get the animator that contains those 2 animations, then when desired key is down (don`t use get key, because it will load the animation every frame while holding) tell the animator to play the desired animation. (make sure the strings are exactly the same)

try this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HeadHit : MonoBehaviour {
 
     public Animator animator; //assign the animator here.
 
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.E)) {
             animator.Play ("Hit0");
         }
         if (Input.GetKeyDown(KeyCode.Q)) {
             animator.Play ("Hit1");
         }
     }
 }


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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Animation only runs in prefab and scene, but not during runtime. 1 Answer

How do I have my animated character have a weapon switching system? 0 Answers

How to have one animation for multiple objects? 1 Answer

Animator not playing onCollisionEnter. 3 Answers

pausing an animation, then running a different animation, and then resuming old animation 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges