- Home /
Question by
mahdimov · Oct 12, 2020 at 09:43 AM ·
scriptableobjectstaticsingletonscriptable object
Unity : Singleton ScriptableObjects resets after play
I have a singleton class that contains general information about my game.
public class GeneralGameData : ScriptableObject
{
private static GeneralGameData _currentGeneralGameData;
public static GeneralGameData CurrentGeneralGameData
{
get
{
if (_currentGeneralGameData == null)
{
_currentGeneralGameData = CreateInstance<GeneralGameData>();
}
DontDestroyOnLoad(_currentGeneralGameData);
return _currentGeneralGameData;
}
}
public string GameName;
public string GameVersion;
}
This class has no presents in the scene.
I also have a window to show and change that
public class GeneralGameDataMenuItem : EditorWindow
{
[MenuItem("CoreMaker/GeneralGameData")]
private static void _generalGameData()
{
GetWindow<GeneralGameDataMenuItem>("GeneralGameData");
}
void OnGUI()
{
GeneralGameData.CurrentGeneralGameData.GameName = EditorGUILayout.TextField("GameName", GeneralGameData.CurrentGeneralGameData.GameName);
GeneralGameData.CurrentGeneralGameData.GameVersion = EditorGUILayout.TextField("GameVersion", GeneralGameData.CurrentGeneralGameData.GameVersion);
EditorUtility.SetDirty(GeneralGameData.CurrentGeneralGameData);
}
}
The problem is that it wont save my changes after i hit play or restart unity. any solutions??
Comment
Best Answer
Answer by Xarbrough · Oct 12, 2020 at 11:53 AM
Unity only saves changes to scenes and assets. In this case, you may want to save the ScriptableObject as an asset and use that instead. See AssetDatabase.CreateAsset.