Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
  • 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

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

Show more comments
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?

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

Show more comments

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.

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

Can I check if this.gameObject was destroyed? 5 Answers

How to destroy only the collided instance of prefab and not the original one? 0 Answers

GameObject not in hierarchy missing reference to a script 1 Answer

Array of objects works half the time! 0 Answers

Can't add Orbital script - "The script needs to derive from MonoBehaviour!" 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