Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Yu_Jin44 · Apr 23, 2020 at 11:59 AM · gameobjectscenedestroyhierarchy

Best workflow for DontDestroyOnLoad method regarding Unity Editor?

I am creating my first game in Unity, nothing big actually, but I have run into a situation, where I would like to hear some advices on best approach. Basically I have Scene 0, which is the main menu, and Scene 1, which is the game. In scene 0 I would like to read in a few values with PlayerPrefs and assign them in a gameobject called GameController. The reason for this is, that the user would be able to change these values in a UI, which is also in Scene 0. In scene 1 I am using these values too to manipulate the game itself. But the GameController gets destroyed on switching scenes, so I would need to read the values in again, which I am trying to avoid. The GameController is actually identical in both scenes, so I thought to use DontDestroyOnLoad, but then I would have to remove it from the scene 1 and that is where I see some issues.

  1. I can not set references to it in other game object in the Inspector.

  2. If I wanted to change something in the game controller I would have to swtich scene

  3. I dont see the GameController in the Hierarchy in Scene 1, which I find confusing sometimes, because working with something, that is not there (or atleast not visible)?!?!

Any ideas how to handle these cases?

Or do you think it would be too big of a hastle and I should just let it reconstruct? Would it be a "normal" process that all games do? I am unexperienced with this.

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 rh_galaxy · Apr 23, 2020 at 06:03 PM

I have a GameManager as Do not destroy (DND) and singleton. Then have the GameManager transfer everything needed from the menu to the gamelevel scene objects through internal static variables in the transition.

 public class GameManager : MonoBehaviour
 {
     public static GameManager theGM = null;
 
     //first code to run
     void Awake()
     {
         //singleton
         if (theGM == null)
         {
             theGM = this;
         }
         else if (theGM != this)
         {
             //enforce singleton pattern, meaning there can only ever be one instance of a GameManager.
             Destroy(gameObject); //<- this makes OnDestroy() be called and we don't want to deinit everything there
             return;
         }
 
         //the rest is done once only...
         DontDestroyOnLoad(gameObject);
 
         //...
     }
 
 }

I also have the camera class as DND to avoid having two or no camera at the scene transition time, and set the camera class from the GameManager for either menu mode or game mode.

But this is just how I do it, I'm not at all sure it's the best thing, but it works.

Comment
Add comment · Show 2 · 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 Yu_Jin44 · Apr 23, 2020 at 07:30 PM 0
Share

This would need to be the first component after the transform component, right? so it would be constructed before all the other scripts (which do the reading with playerprefs) and destroyed if already loaded?

Edit: I just tryed, if you assign the gamecontroller from the game scene to a component of another gameobject in inspector as reference, it is lost once the game starts. Actually that way you assign the gamecontroller from the game scene, but once the game runs it is not there, there is the gamecontroller from the menu scene ins$$anonymous$$d, which is a different gameobject. This means that your answer is not what I am searching for.

avatar image rh_galaxy Yu_Jin44 · Apr 23, 2020 at 08:27 PM 0
Share

You will have to attach the Game$$anonymous$$anager script to an empty gameobject created in the first scene you run.

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

218 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

Related Questions

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

How to destroy all gameobjects active in hierarchy? 1 Answer

Using empty gameobjects as "folders" in scene hierarchy panel 1 Answer

DontDestroyOnLoad Slowing Loading Down 0 Answers

Deleting GameObjects 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