• 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
0
Question by XoetziX · Apr 11, 2021 at 10:41 AM · destroy objectfindobjectoftype

FindObjectOfType returns destroyed object (leading to MissingReferenceException)

Hello all,

I have a problem when reloading a scene, after clicking the restart button and I invested maaanny hours now to find the reason without having success. So I really hope you can help me! :-)

My setup:

  • GameManager

--- Singleton (destroying new intances)

--- set to DontDestroyOnLoad

--- gets himself a reference to a levelUIManager within the Update() method. This reference points to a actually destroyed instance after reloading the scene (see debug output below).

 private LevelUIManager levelUIManager;
 private void Update()
 {
     if (!gotOnce)
     {
         levelUIManager = FindObjectOfType<LevelUIManager>();
         Debug.Log("-------Game Manager Update-------- REF: " + levelUIManager.uuid);
         gotOnce = true;
     }
 }


    public void GameOver()
     {
         Debug.Log("---------GameOver--------LevelUIManager- UUID: " + levelUIManager.uuid);
         if (gameHasEnded == false)
         {
             gameHasEnded = true;
             levelUIManager.SetGameOver();
             levelUIManager.ShowGameOverUI();
             Debug.Log("Game Over");
         }
     }

  • LevelUIManager

--- for debug purposes: generates a UUID within Awake() in order to know which intance I have

--- onDestroy(): shows the UUID of the destroyed instance .

 private void Awake()
 {
     uuid = Guid.NewGuid();
     Debug.Log("--------LevelUIManager - Awake-------UUID: " + uuid);
 }

 private void OnDestroy()
 {
     Debug.Log("Level UI Manager DESTROYED - UUID: " + uuid);
 }

If the restart button is hit, the Restart() of the GameManager is called:

 public void Restart()
 {
     GameManager.Instance.gotOnce = false;
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }

  • PlayerCollision

--- within the OnCollisionEnter() I call the GameOver() method of my singleton instance of my GameManager

 GameManager.Instance.GameOver();


.

Output in the console shows that the wrong instance is returned within the seconds Game Manager Update and then used within the GameOver() which causes the problem I actually come from, a MissingReferenceException, which is logical if it uses a destroyed object...:

----------LevelUIManager - Awake---------UUID: a026f582-964d-4657-93dc-c222b24cfa7b ---------Game Manager Update-------- REF: a026f582-964d-4657-93dc-c222b24cfa7b ---------Game Manager Update-------- REF: a026f582-964d-4657-93dc-c222b24cfa7b
Level UI Manager DESTROYED - UUID: a026f582-964d-4657-93dc-c222b24cfa7b ---------LevelUIManager - Awake---------UUID: 3fa4f491-9038-40c5-9892-934ed9eea53b ---------GameOver--------LevelUIManager- UUID: 4fd23d3c-b0da-4d93-b29c-461678f36841

.

The only thing which caught my attention is the order in which the debug statments occur. Based on my understanding of the "order of execution for event functions" of MonoBehaviour classes I would have expected the following order:

  • LevelUIManager Awake

  • GameManager Update

  • LevelUIManager Destroy (first action of the loading a new scene process)

  • LevelUIManager Awake (as part of the instantiation, I expected this bevor the following update)

  • GameManager Update

.

Is this understanding correct, and only the debug log statements have a wrong order, or can it be that the Update() of the GameManager really is executed before the new LevelUIManager is created (maybe because I dont destroy it?!?)

.

By the way: Formatting the text here to be readable is almost as challenging as game development... :-(

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 logicandchaos · Apr 11, 2021 at 04:31 PM 0
Share

where is the part where you get the reference? That is what I need to see. Awake() and Start() only run the 1st time the object is activated, if you want something to run every time it's activated put it in OnEnable(). Look into pooling, rather than destroy instances, deactivate them, saves computation time and then you also avoid destroying references.

I would also look into using scriptable object architecture over singletons..

avatar image XoetziX logicandchaos · Apr 11, 2021 at 06:33 PM 0
Share

I get it within the Update() method of the GameManager. See the first code extract above. if (!gotOnce) levelUIManager = FindObjectOfType();

After searching for a solution for hours I also saw the advice to use scriptable object instead of singletons. I did not work on this topic so far, but I am anyway ambitious to understand why I have this problem. It may help me in the future to circumvent other problems.

0 Replies

· Add your reply
  • Sort: 

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

115 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

Related Questions

How to change color on multiple game object when button is clicked 1 Answer

Cannot Destroy() object, created via GameObject.CreatePrimitive (others destroyed as should) 1 Answer

Reset prefab instead of destroying and instantiating again 1 Answer

destroy object and play sound? 9 Answers

Farming game Need Help with planting and picking 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