- Home /
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|% }`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeadHit : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E)) {
animation.Play ("Hit0");
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q)) {
animation.Play ("Hit1");
}
}
}
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.
I have no idea how to set up an animator, do you have some kind of video/tutorial that i can learn from?
https://m.youtube.com/watch?v=7RtRiniiW0c
YouTube is your friend
Answer by OmerDoron · 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");
}
}
}
Answer by chetan312 · Dec 16, 2020 at 05:42 AM
hello everyone, i know this is very old question , but i had similar problem few days ago. So in my case , i accidentally created a script called "Animator". Now my all animations in project called this script (this script is empty), so as result i got lot of errors . so just look in your project, if you had also an script named as "Animator" which is created by you and delete it because it is conflicting the pre made classes of unity. Sorry for my bad English. Hope this is useful.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
pausing an animation, then running a different animation, and then resuming old animation 0 Answers
Controlling an Animation's playback through the Animator Component? 0 Answers
my animation sprite is been block by the background 1 Answer
How do I have my animated character have a weapon switching system? 0 Answers