• 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 Michellia · Mar 25, 2013 at 01:14 AM · scenebooleaninstancedonotdestroy

Boolean being chaned on new scene load

We have a component class to save all our variables we need for all scenes.

problem.

When we leave the scene all boolean's are correct.

the base class is set to notdestroyonload

when we enter the new scene all boll's int he child class are reset to false.

GameObject->Gamedata->instance->settings.

I have attempted to set settings to donotdestroy but unity say no, I must have settings as a separate class/object because I am serializing it.

any ideas why its being reset, all our boolean's.

if you need more data please ask.

thanks all

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 Benproductions1 · Mar 25, 2013 at 03:37 AM 0
Share

A bit more clear explanation of your setup would probably help in this situation

avatar image liamcary · Mar 25, 2013 at 05:24 AM 0
Share

Can't really help without more info, but keep in $$anonymous$$d that objects with DontDestroyOnLoad will call their OnEnable and OnDisable functions between scenes. $$anonymous$$ake sure you're not resetting the variables in those functions.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Michellia · Mar 25, 2013 at 11:10 PM

ok

It Seems to be that the child classes are all reset to false/null on everything with DoNotDestroyOnLoad().

Our child class is not inherited from MonoBehaviour its just a c# class. we have it this was because with MonoBehaviour you cannot serialize our variable to be saved. also these class are have been developed over time and have always worked in other apps.

The DoNotDestroyOnLoad is not called in the child class its called in the parent class.

This is the GameData Class, Settings and Save Data are stranded C# scripts not inheriting from mono behavior, and NOT utilizing a singleton system:

 using System.IO;
 using System;
 
 public class GameData : MonoBehaviour
 { // Start GameData.
     
     // Settings Data:
     public SettingsData settings;
     
     // Save Data:
     public SaveData saveData;
     
     // High Score List:
     private GenericManager<int,float> highScoreList;
     
     #region XML related Data
     // High Score XML/Saving data:
     public bool HSIsSaveable = DataGlobals.HSIsSaveableDefault;
     public string HSXmlElementName = DataGlobals.HSXmlElementNameDefault;
     public string HSXmlNamespace = DataGlobals.HSXmlNamespaceDefault;
     public string HSSavePathAndName = DataGlobals.HSSavePathAndNameDefault;
     
     
     // Settings XML/Saving data:
     public bool settingsIsSaveable = DataGlobals.settingsIsSaveableDefault;
     public string settingsXmlElementName = DataGlobals.settingsXmlElementNameDefault;
     public string settingsXmlNamespace = DataGlobals.settingsXmlNamespaceDefault;
     public string settingsSavePathAndName = DataGlobals.settingsSavePathAndNameDefault;
     
     // Settings XML/Saving data:
     public bool saveDataIsSaveable = DataGlobals.saveDataIsSaveableDefault;
     public string saveDataXmlElementName = DataGlobals.saveDataXmlElementNameDefault;
     public string saveDataXmlNamespace = DataGlobals.saveDataXmlNamespaceDefault;
     public string saveDataSavePathAndName = DataGlobals.saveDataSavePathAndNameDefault;
     #endregion
     
     // Local Variables:
     short i;
     short listCount;
     
     #region Properties
     
     
     #endregion
     
     #region Instance
     
     private static GameData Instance;
     
     public static GameData getInstance()
     {
         if( Instance == null )
             Instance = new GameData();
         
         return Instance;
     }
     #endregion
     
     #region Start
     
     void Start ()
     {
         // Checking for any aditional GameData Classes and destroying as needed:
         if ( FindObjectsOfType( typeof( GameData )).Length > 1 )
         {
             Debug.Log( "Only one instance of GameData is allowed, Distroying thsi instance." );
             DestroyImmediate(gameObject);
         }
         else if( Instance == null )
         {
             Instance = this;
             Initialize();
             DontDestroyOnLoad( transform.gameObject );
             DontDestroyOnLoad( Instance );
             
         }
         //else
         //{
             //Debug.Log( "Error only one instance of this script can exsist at any one time" );
             //throw new ArgumentNullException();
         //}
     }
     #endregion
     
     // Use this for initialization
     public void Initialize () 
     { // Start Initialize.
         
         #region Settings up XML related variables
         // High Score XML/Saving data:
         HSIsSaveable = DataGlobals.HSIsSaveableDefault;
         HSXmlElementName = DataGlobals.HSXmlElementNameDefault;
         HSXmlNamespace = DataGlobals.HSXmlNamespaceDefault;
         HSSavePathAndName = DataGlobals.HSSavePathAndNameDefault;
         
         // settings XML/Saving data:
         settingsIsSaveable = DataGlobals.settingsIsSaveableDefault;
         settingsXmlElementName = DataGlobals.settingsXmlElementNameDefault;
         settingsXmlNamespace = DataGlobals.settingsXmlNamespaceDefault;
         settingsSavePathAndName = DataGlobals.settingsSavePathAndNameDefault;
         
         // settings XML/Saving data:
         saveDataIsSaveable = DataGlobals.saveDataIsSaveableDefault;
         saveDataXmlElementName = DataGlobals.saveDataXmlElementNameDefault;
         saveDataXmlNamespace = DataGlobals.saveDataXmlNamespaceDefault;
         saveDataSavePathAndName = DataGlobals.saveDataSavePathAndNameDefault;
         
         #endregion
         
     
         // Setting up the game settings:
         settings = new SettingsData();
         settings.Initialize( settingsXmlElementName, settingsXmlNamespace, settingsSavePathAndName );
         //settings = new SettingsData( settingsXmlElementName, settingsXmlNamespace, settingsSavePathAndName );
         
         // Setting up the save data:
         saveData = new SaveData( saveDataXmlElementName, saveDataXmlNamespace, saveDataSavePathAndName );
         
         // Attempting to laod an XML file and set the high scores
         // If this does not work then we load the default scores:
         #region High Score Loading/Gernerating
         highScoreList = new GenericManager<int,float>( HSIsSaveable, HSXmlElementName, HSXmlNamespace, HSSavePathAndName );
         if( !highScoreList.loadObjectList( HSIsSaveable, Path.Combine( Application.dataPath, HSSavePathAndName ), HSXmlElementName, HSXmlNamespace ) )
         {
             // Setting a default high score list:
             listCount = DataGlobals.highScoreDefaultListSize;
             for( i = 0; i < listCount; i++ )
                 highScoreList.addObject( i, DataGlobals.highScoreDefaultList[i] );
             
             // Saving the Default high Score List:
             highScoreList.saveObjectList( Path.Combine( Application.dataPath, HSSavePathAndName ), HSXmlElementName, HSXmlNamespace );
         }
         #endregion
         
     } // End Initialize.
 } // End GameData.
 
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.

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

12 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

Related Questions

[C#] Checking InstanceOf? 2 Answers

Object Painter Editor Help 1 Answer

Changing a bool on multiple instances of the same script 1 Answer

How to Set a bool to Individual Scenes (C#) 1 Answer

Multiple world spaces? 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