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 pigaroos · Sep 05, 2019 at 07:54 PM · dontdestroyonloadpublic variablereferences

How to use DontDestroyOnLoad() only with certain scenes

Hello everyone, recently I substituted my PlayerPrefs method of saving data to a custom Save/Load system. However, the SaveData() method requires references to other scripts in the main menu, which is totally fine. Though in the level itself, if I want to save the data, I wouldn't have the scripts to reference (because they reside in the main menu). If I use DontDestroyOnLoad(), I've noticed that the original references to objects in the main menu (where the scripts are originally from) get undone. So if I loaded the level, and went back to the menu, all the public variables would be unconnected. How do I take the objects to the next scene, but if returned to the MainMenu, use the objects that have the references? Thanks for any help.

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
Best Answer

Answer by Diogo_NullMoon · Sep 06, 2019 at 11:16 PM

I create two GameObejcts, one just have a simple instantiete code...`

 GameObject gameController;
 public GameObject gameControllerPrefab; 
 public Transform gameControllerPrefabTransform;

 void Awake() {
     // Search for a GameController
     gameController = GameObject.FindWithTag("GameController"); 
     
     if (gameController == null){ // if dont have one, create it
         gameController = gameControllerPrefab;
         Instantiate(gameController, gameControllerPrefabTransform.position, gameControllerPrefabTransform.rotation);
     }
 }`

and the second have the "DontDestroyOnLoad()"

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 pigaroos · Sep 07, 2019 at 12:06 AM 0
Share

$$anonymous$$aking prefabs of my required scripts to then instantiate them on my in-game scene sounds like a great idea! Thank you so much, trying this tomorrow.

avatar image pigaroos · Sep 07, 2019 at 04:02 PM 0
Share

Instantiating the objects in the in-level scene to then reference then and use my SaveData() method did the trick! Thanks.

avatar image
1

Answer by surfuay · Sep 06, 2019 at 02:13 AM

whatever script is your Don'tDestroyOnLoad needs to contain a reference to any and all variables that you want to persist

IE score

Main Menu has a high score display/ don't destroy has a saved score/ level has a current score

so it would look something like this

Main Menu (this will update your high score every time your Menu Scene Loads in and only then)

 public int HighScore = 0; (can also be written public int HighScore;  or what ever you want)

  private void Start()
  {
       if (SavedScore > HighScore) 
       {
             HighScore = SavedScore;
      }
  }

DontDestroy

   public int SavedScore = 0;
  //that's all here, you don't want you persistent data holder to keep doing a bunch of stuff in other scenes, you just want it to be told what to hold by other scripts and you want other scripts to ask it what information it has, so your GAME LEVEL scene will tell this script what to hold,  your MAIN MENU scene will ask what information this script contains.

Game Level (as your game updates its score, at some point you'll want it to tell the don't destroy script what the final score is, so either at death or even as it progresses)

  public int CurrentScore = 0; (or public int CurrentScore;)
  public void OnDestroy() (or game over or whatever instance you want to tell the don't destroy what the score is)
 {
       if (CurrentScore > SavedScore)
       {
             SavedScore = CurrentScore;
        }
  }


That's basically it, I would look up a tutorial on singularities or serialization or both. It will be super helpful as you go, but this is the basic gist. There's a few things I left out that you'll need from a serialization tutorial. If I typed it out this answer would be grossly longer. Best of Luck!

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 pigaroos · Sep 06, 2019 at 09:08 PM 1
Share

@surfuay Hello, thank you so much for the answer. Though this still wouldn't let me save from the other scenes as the scripts needed to reference are on the main menu scene.

avatar image surfuay · Sep 08, 2019 at 05:00 PM 0
Share

no worries, the method I did was very long compared to the above answer. my method changed where you referenced things. you wouldn't have been referencing the main menu anymore, rather everything would reference the don't destroy object.

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

111 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

Related Questions

dontdestroyonload object references 0 Answers

How to keep the same gameobject with different transform values in different scenes 1 Answer

Finding nearest and with highest health points 1 Answer

DontDestroyOnLoad - is it intended behavior? 3 Answers

...Why does this FAIL? - DontDestroyOnLoad()?? 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