- Home /
Issue with animator not working when key is pressed - Script and Animator communication - 2D - MissingComponentException
Hello I was hoping somebody may be able to help as I have tried searching, poking and looking on forums for tuts an answers.
I am having issues with getting this to work so here goes. When the space bar is press, a blast wave will shoot out in a 2D radius - (game is 2d) The blast isnt attached to the player, it is free floating ine the scene, if you attach it to the player, the same error comes up. The code is also attached the to 'Player'
If any one could help i would be happy.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAtk : MonoBehaviour {
public Animator anim;
// Use this for initialization
void Start (){
anim = GetComponent.<Animator> ();
}
void Update()
{
if(Input.GetKeyDown("space"))
{
Attack();
}
}
void Attack()
{
Debug.Log("blam!");
// elements of what "Attack" does eg. sends rigid bodies flying,
// enemies flying in a radius around the player.
anim.Play("idle");
//idle is a state in the mechanim
}
}
I have also attached this.
MissingComponentException: There is no 'Animator' attached to the "Player" game object, but a script is trying to access it. You probably need to add a Animator to the game object "Player". Or your script needs to check if the component is attached before using it.
Thanks,
it says that your object doesn't have the animator component,so you cannot play the animation. i guess you forgot to attach the animation to this gameobject,casue it will automatically add the animator component.
Hello, the strange thing is I'm pretty certain i did. I created the Animation + plus the 2 states in the state machine (idle and animation) I also tried the drag the 'animator' onto the public variable field inside Unity - on the script component and it still wouldn't recognise it. I may have to start again and retrace my steps with this one. I followed a tutorial online. - for which ever reason I couldn't get the GetComponent to communicate with the animation in the Unity Scene view.
hi, in this case,maybe you attached this script to another object !!!!check this (you should attach the script to the object that has the animator component)
Answer by Vega4Life · Jul 18, 2019 at 04:21 PM
Could this be the problem? You have "anim = GetComponent.<Animator> ();"
There is a dot in there. It should be "anim = GetComponent<Animator> ();"
Nice spot, but apparently this hasn't changed the issue and for some odd reason that full-stop is only in the code pasted into the forum here.
I have a suspicion that it could a communication issue between the script and the animator/object. Being new to C-Sharp, I'm just not entirely sure how to fix it.
$$anonymous$$issingComponentException: There is no 'Animator' attached to the "Player" game object, but a script is trying to access it. You probably need to add a Animator to the game object "Player". Or your script needs to check if the component is attached before using it.
Answer by blinkafrootable · Jul 19, 2019 at 05:45 PM
If you intend on dragging an animator into the script through the Unity Editor (as you said you did in one of your post's comments) then you don't need to call GetComponent in the first place. Try dragging your desired animator into the script and getting rid of the GetComponent from the code and see how it goes. Hope this ends up helping @jimm84
Answer by jimm84 · Jul 23, 2019 at 06:45 AM
Thank you all, came at this from a different approach this is what I changed :-
The code, and what the code was attached to - now the child object
This appears to have fixed it, i hope this will come useful.
This script was attached to the child / blast / not the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlastWave : MonoBehaviour {
public Animator anim;
// Use this for initialization
void Start (){
anim = GetComponent<Animator> ();
}
void Update()
{
if(Input.GetKeyDown("space"))
{
// when space is pressed play EMP animation
anim.Play("EMP");
Debug.Log("blam!");
}
}
}
Your answer
Follow this Question
Related Questions
OnStateEnter isn't updating in the correct order with normal update? 0 Answers
Mecanim. Why cant I get current state name? 1 Answer
Mecanim transition retargeting and conditional exit to any state? 0 Answers
Transition to multiple states from Entry Node in Unity 5 1 Answer
Mecanim state that keeps last pose? 0 Answers