• 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 ashkatchen · Nov 26, 2021 at 08:10 PM · gameobjecterror messagemissingreferenceexception

MissingReferenceException: The object of type 'Animator' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

I have an endless runner game and my player can respawn after wathing an ad. But after the player goes to the market page, or just tries again without reviving, unity gives the error

"MissingReferenceException: The object of type 'Animator' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.". I'm using LoadScene function to restart the game or coming back from the market.

This is my code for restarting the game after death.

     public void TryAgain()
     {
         PlayerPrefs.SetInt("TotalMoney", numberOfCoins + PlayerPrefs.GetInt("TotalMoney"));
         SceneManager.LoadScene(0);
         scoreMultiplier = 1;
         numberOfBullets = 0;       
     }

And this is the one that works after the ad ends. But error says it cant find the animator in here.

     public void Revived()
     {
         moveSpeed = 10;
         PlayerManager.scoreMultiplier = 1;
         death = false;
         animator.SetTrigger("Revive");
         deathStuff.SetActive(false);
     }








Comment
Add comment · Show 23
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 MilitaryG · Nov 26, 2021 at 08:47 PM 0
Share

Just set the gameObject to dont destroy on load like this:

 DontDestroyOnLoad(this.gameObject);//the animator gameObject
 SceneManager.LoadScene(0);

This is how unity understands:

 Scene 0 go1 on RAM 0001
 Scene 1 go1 on RAM 0002

 Than in scene 1 you are trying to access 0001 witch is destroyed.
avatar image ashkatchen MilitaryG · Nov 26, 2021 at 09:11 PM 0
Share

The problem is, the stuff is not actually destroyed. They are there and even referenced in the script. But when the script is trying to use them, it gives me this error.

avatar image MilitaryG ashkatchen · Nov 26, 2021 at 09:38 PM 0
Share

Just looking more clearly at your error

This script has nothin to do with animator except scene

The animator is destroyd

avatar image ashkatchen MilitaryG · Nov 26, 2021 at 09:54 PM 0
Share

It works perfectly until i reload the scene. Then it starts to give the error but the animator is still there. It gives the same error but with the "GameObject" version for another referenced object.

Show more comments
avatar image ashkatchen MilitaryG · Nov 28, 2021 at 09:16 AM 0
Share

This is very clever. So can you tell me how to fix it? Because i'm lost.

avatar image MilitaryG ashkatchen · Nov 28, 2021 at 09:34 AM 0
Share

The animator GameObject find it and attach it

Don't know where is it can't help if I'm blind

avatar image ashkatchen MilitaryG · Nov 28, 2021 at 11:15 AM 0
Share

It didnt worked. The thing is, i'm instantiating a new player prefab in each reload. And the player has a script that grabs everything that it needs. So my question is: If everything is spawned form scratch in each reload, whats the difference between the first time and the second time? When i open the game for the first time, it works. Bu when it reloads, it doesnt. But basically, all of the stuff starts from the scratch in both times. So if we can, i can use a live help from you.

avatar image MilitaryG MilitaryG · Nov 28, 2021 at 11:35 AM 0
Share

Try

In TryAgain()

Grab script in witch is giving you error

And set it don't destroy on load as written above

avatar image ashkatchen MilitaryG · Nov 28, 2021 at 11:42 AM 0
Share

It didnt worked and i just noticed something. There are some other functions in this script that uses the same animator. And they dont give any errors. Only this specific function is having an error with the animator. Is that normal?

avatar image MilitaryG ashkatchen · Nov 28, 2021 at 11:52 AM 0
Share

Yes they do the error to afaik.

Everything that tries to access the animator when it's destroyed will give error but sometimes it points you elsewhere

Show more comments
avatar image MilitaryG · Nov 28, 2021 at 12:33 PM 0
Share

Ok I won't be able to get anything done if I won't see full codes

Edit your post and post full codes with using included.

If that isn't done I can't help.

avatar image ashkatchen MilitaryG · Nov 28, 2021 at 01:07 PM 0
Share

Check it out.

avatar image MilitaryG · Nov 28, 2021 at 01:26 PM 0
Share

Ok try this:

 public class Running : MonoBehaviour{

     void Start(){
         DontDestroyOnLoad(this.gameObject);
         DontDestroyOnLoad(animator);
     }
 }
avatar image ashkatchen MilitaryG · Nov 28, 2021 at 01:42 PM 0
Share

Still doesnt work.

avatar image MilitaryG MilitaryG · Nov 28, 2021 at 01:45 PM 0
Share

And if you add:

 DontDestroyOnLoad(this);

Might be enough

avatar image ashkatchen MilitaryG · Nov 28, 2021 at 01:47 PM 0
Share

Getting the same error. Somehow, Revive() function cannot find the animator when other ones can.

avatar image MilitaryG · Nov 28, 2021 at 01:35 PM 0
Share

4640 is my discord id don't call me we can chat if you want

avatar image ashkatchen MilitaryG · Nov 28, 2021 at 01:38 PM 0
Share

I thought i can share my screen with you so we can work this out faster.

avatar image ashkatchen MilitaryG · Nov 28, 2021 at 01:42 PM 0
Share

And i think i need a name too for discord

avatar image MilitaryG ashkatchen · Nov 28, 2021 at 01:47 PM 0
Share

MilitaryG#4640

avatar image ashkatchen MilitaryG · Nov 28, 2021 at 01:50 PM 0
Share

Sent the request

2 Replies

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

Answer by ashkatchen · Nov 28, 2021 at 05:27 PM

I wrote

         if(animator == null)
         {
             animator = GetComponentInChildren<Animator>();
         }

in the update function and it worked.

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 jeff13gaydos · Apr 08 at 10:20 AM

For anyone looking for a less "patchy" solution, my errors had to do with not unsubscribing some callbacks in scripts that were in an observer pattern (you should do this OnDestroy), and then also some scriptable objects were referencing destroyed objects (since scriptable objects are persistent). The above solution will work in all (if not most) situations regardless.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

241 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Array of objects works half the time! 0 Answers

Can't add Orbital script - "The script needs to derive from MonoBehaviour!" 0 Answers

BCE0019: 'gameObject' is not a member of 'Object'. 1 Answer

if (Something != null) not good enough 2 Answers

Unity bug gives false error messages or doesen't work without a Debug.Log() line present 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