Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ovidiubuligan · Jun 21, 2015 at 01:27 PM · gameobjectsingletonondestroy

Singleton , DontDestroyOnLoad() and scene reloading

I am having trouble with DontDestroyOnLoad() and scene reloading. I have this script :

 public class SingleInstance : MonoBehaviour
 {
     private static Dictionary<string, GameObject> instances = new Dictionary<string,GameObject>();
 
     void Awake()
     {
         GameObject inst = null;
         instances.TryGetValue(this.name, out inst);
         if (inst == null)
         {
             DontDestroyOnLoad(this.gameObject);
             instances[this.name] = gameObject;
         }
         else
         {
             DestroyImmediate(gameObject);
         }
     }
 }

I aplied this script to my UIManager object which is part of a simple 3 object Hierarchy :

UIManager

  -ImageLoader
  -Canvas

(this 3 object hierarchy is a prefab and was dragged and dropped in the scene). The problem is that after reloading the scene both child objects (the Canvas and ImageLoader) get OnDestroyed called when reloading the scene. In the inspector they still look ok but in code where I access the canvas it says that it is Destroyed with this messge:

MissingReferenceException: The object of type 'Canvas' 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 don't have any Ideea why the children get "flagged" as destroyed becaus the SingleInstance script only destroys the toBeCreated Object.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoSLTS · Jun 21, 2015 at 05:40 PM

Note that Unity calls "OnDestroy" automatically (http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html), OnDetroy*ed* is a custom function that you'll have to call manually.

I just tested with your code writing and OnDestroy function for the childs that prints their names, and it's not being called after reloading the scene with:

 Application.LoadLevel(Application.loadedLevel);

Now, for your issue, I guess you have some other object (lets call it "obj") in your scene that has a reference to the Canvas GameObject. When you reload the scene Unity creates all the objects in the scene, setup the references (obj has a reference to the newly created Canvas inside the newly created UIManager) and on the Awake face destroys the newly created UIManager with the Canvas child. Now, obj has an invalid reference, it's pointing to a Canvas that doesn't exists, the Canvas that wasn't destoyed is a different object from the previous scene.

One solution to your problem would be to replaces those static references to childs of singletons with finds in scripts. Instead of setting the reference to the Canvas in the inspector, look up for a game object called "Canvas" in some Awake or Start function.

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

21 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Does the Singleton GameObject need to be in all scenes where i need to access it? 2 Answers

MonoBehaviour.OnDestroy() how it works/ how to use it? 1 Answer

OnDestroy() callback in Editor upon deleting selected GameObject - del key 5 Answers

I cant delete multiple instantiated prefabs 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