Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Sep 28, 2017 at 08:39 PM by marcsebas for the following reason:

And the bug was not very related with my script in the end, but with a dumb mistake.

avatar image
0
Question by marcsebas · Sep 28, 2017 at 11:54 AM · animationnullreferenceexception

Getting variable from animation returns Null

Hello, This is my first time asking a question in here and in general, therefore I apologize if I couldn't explain myself properly and I ask for understanding. With this said, I am very thankful to anyone who tries to help me solve this issue!

I am designing a game where the enemes will walk towards the player and, on contact, attack it and deal damage. Since I have to time when the attack takes place, I decided to write this logic within the animation script (as you'll see, please also tell me if it's better to do everything in the Enemy controller script instead).

Within the Enemy class, I have a public variable

 public GameObject target;

and I do not define it in the inspector, since it will be given a value once it collides with a target (for now it's only the player controller). When they collide, target gets a value. So, to the important part:

 public GameObject GetTarget(){
     print(target); //Prints the game object of the target correctly.
     return target;
 }

When the Enemy is in contact with its target, it prints the correct target, however, when the value in the animation script that I get is Null.

So, here you go, this is the animation script:

 public class EnemyAttack1HAnimation : StateMachineBehaviour {
 public float attackDelay;
 private float nextAttack;
 private GameObject EnemyObject;
 override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     nextAttack = Time.time + attackDelay;
     EnemyObject = animator.gameObject.transform.parent.gameObject;
 }

 override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     if (Time.time > nextAttack){
         nextAttack = Time.time + attackDelay;
         PlayerController target = EnemyObject.GetComponent<Enemy>().GetTarget().GetComponent<PlayerController>();
         Debug.Log(target); //Null <- This is the issue (Also Null if I don't call the GetComponent, of course)
     }
 }

 // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
 override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     animator.SetBool("attack", false);
 }

Thanks again for your help and I'm looking forward to learning further about Unity and C#! =)

Comment
Add comment · Show 2
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 FortisVenaliter · Sep 28, 2017 at 05:59 PM 0
Share

$$anonymous$$ind of a dumb question here, but your player object does have a PlayerController component on it, right?

avatar image marcsebas FortisVenaliter · Sep 28, 2017 at 08:37 PM 0
Share

Yes, it is attached, but thanks to your comment I checked something out and indeed the problem was related to it. The thing is, the game is a tower defense, and the Enemy is colliding with the Tower, not the player (the character on the tower). Obviously, the tower has no player controller script whatsoever. So, I did such a dumb mistake of overseeing that. I feel so stupid now.

Thanks for your reply, since it helped me out of my tunnel vision =)

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Please Help!! NullReferenceException when clicking restore default pose 0 Answers

NullReferenceException on animation clips 3 Answers

Can't get boolean of an animator from other class 0 Answers

Why am I getting a NullReferenceException while trying to set a gameobjects animation frame? 0 Answers

nullReferenceException error 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