Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by jimm84 · Jul 18, 2019 at 03:27 PM · buttonanimatormecanimgetcomponentstate-machine

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,

Comment
Add comment · Show 4
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 PersianKiller · Jul 19, 2019 at 03:14 PM 0
Share

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.

avatar image jimm84 PersianKiller · Jul 19, 2019 at 03:22 PM 0
Share

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.

avatar image PersianKiller jimm84 · Jul 21, 2019 at 04:01 AM 0
Share

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)

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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> ();"

Comment
Add comment · Show 1 · 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 jimm84 · Jul 19, 2019 at 10:14 AM 0
Share

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.

avatar image
0

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

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 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!");
 
         }
     }
         
 }

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

160 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 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 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 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 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 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 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 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

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


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