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 /
  • Help Room /
avatar image
0
Question by Grateyfratez · Sep 04, 2015 at 10:22 PM · unity 5animatornullreferenceexception

What could cause a script's reference to an Animator to become null?

I have a prefab that has an Animator and a script controlling the animator. The animator is connected to the script in the editor. The prefab is a character, and I instantiate 2 of them to battle each other. I run through a couple of abilities just fine; their animations are both working. But eventually I get a null reference exception due to animator being null in my script. I don't ever assign the animator variable via script, I link it in via the inspector and then only reference it from there on out. After the null reference occurs I check the instantiated copy of the prefab and sure enough the animator slot on my script is "none". Any ideas?

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 Grateyfratez · Sep 05, 2015 at 04:54 PM 0
Share

I added a property that everything now goes through to get the animator.

     [SerializeField]
     private Animator animator;
     private Animator TheAnimator
     {
         get 
         { 
             if(animator == null)
             {
                 Debug.LogError("Animator is null!");
                 animator = GetComponent<Animator>();
                 if(animator == null)
                 {
                     Debug.LogError("Animator is STILL null after attempting to get the component!");
                 }
             }
             return animator;
         }
     }

While it has never been null after trying to call GetComponent, it frequently loses the reference and has to call GetComponent again. I have no idea why though, there's nothing in my code that touches the animator besides getting and setting its parameters

2 Replies

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

Answer by Grateyfratez · Sep 06, 2015 at 04:45 PM

Turns out I was editing an animation when I linked the animator to the script. Apparently linking references is something the animation can automate.

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 getyour411 · Sep 05, 2015 at 05:57 AM

That's how prefabs are. You have to set the external references/Getcomponent calls via it's Awake/OnEnable/Start.

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 Grateyfratez · Sep 05, 2015 at 04:02 PM 0
Share

The animator and the script are on the same gameobject, so I link them in the editor via drag and drop and apply it to the prefab. The reference remains intact for a while during gameplay and then eventually it just disappears.

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

28 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

Related Questions

Unity animation doesn't work after time delay or when accessed from another script? 0 Answers

MobController 1 Answer

FPS Controller bug? 0 Answers

NullRefrenceException - Cant find the cost 1 Answer

Animation State not working 1 Answer


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