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 kakulukiya · Jan 24, 2014 at 07:23 PM · scene-loadingsingleton

Using Toolbox Singleton Script and Reloading Scene

I was trying to use the script here - http://wiki.unity3d.com/index.php/Toolbox. I attached it to a GameObject and was putting my global vars. It works well. But if I reload the scene, I get a warning "[Singleton] Instance 'Toolbox' already destroyed on application quit. Won't create again - returning null." And I can't access my global vars again. So how do I use this script and different scenes together ?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JASlaughter · Mar 09, 2014 at 05:15 PM

Add a method to the Singleton class:

         public void Reload()
         {
             applicationIsQuitting = false;
         }

Then call it from Toolbox.Awake() using this.Reload();

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 Bunny83 · Mar 09, 2014 at 05:28 PM 0
Share

Sorry, but that makes not much sense. applicationIsQuitting has been implemented to prevent recreation of the instance at edit time when using ExecuteInEdit$$anonymous$$ode. Setting it to false in Awake would render the whole point of this variable useless.

As i said in my answer the singleton implementation is missing a call to DontDestroyOnLoad in the case when the instance already exists in the scene.

avatar image
0

Answer by Bunny83 · Jan 24, 2014 at 08:17 PM

Well, the singleton implementation which is used by the toolbox is missing a DontDestroyOnLoad in the "else" branch. The else branch is the case when you already have an instance in the scene. Since DontDestroyOnLoad is not called in this case, the instance get destroyed on scene change and due to the OnDestroy check never restored.

All you have to do is to add this line to the else branch of the singleton:

     DontDestroyOnLoad(_instance.gameObject);

Comment
Add comment · Show 3 · 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 kakulukiya · Jan 24, 2014 at 10:43 PM 0
Share

I tried that. I get the same error.

When the scene reloads, applicationIsQuitting is set to true as the gameobject which the ToolBox script is attached to is destroyed. And it is never reset to false. Anytime I call Toolbox.Instance, it will return null from now on and give me that warning. What happens now is I have 2 game objects with Toolbox script attached to them...one with my global vars and one without for some reason.

Am I using Toolbox wrong ?. Shouldn't I attach it to a gameobject ?

avatar image Bunny83 · Jan 25, 2014 at 03:13 AM 0
Share

Well, it should work with DontDestroyOnLoad, however when testing in the editor there's another problem with "applicationIsQuitting". Since it's a static variable it will survive the playmode change. In a build that problem doesn't exist.

The singleton implementation should set applicationIsQuitting to false when it's created (in Awake for example but the constructor might work as well).

All in all the whole toolbox thing and it's underlying singleton class is not that well implemented.

avatar image kakulukiya · Jan 26, 2014 at 06:23 PM 0
Share

So what's a good way if I want a global game manager kind of thing ?

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

How to create a main menu with singleton pattern? 2 Answers

Button wont run sceneloader coroutine-calling functions, but can run it's other functions 0 Answers

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

Coroutine does not work when scenes changes 1 Answer

How to have multiple scenes concurrently runnning 3 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