- Home /
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
A bit more clear explanation of your setup would probably help in this situation
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095025im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Preventing first instance of object from starting on screen 1 Answer
[C#] Checking InstanceOf? 2 Answers
Object Painter Editor Help 1 Answer
Changing a bool on multiple instances of the same script 1 Answer
Multiple world spaces? 0 Answers