Scriptable Objects Resets on Scene Change - Shorotshishir/Learnings GitHub Wiki
Scriptable Objects Resets on Scene Change
While trying to store my game score in a Scriptable object and later show it in the main Screen, i found my data is not in the Scriptable object anymore, but if The Scriptable Object is kept Serialized in the editor i.e. view the scriptable object from inspector, the data persists across the scene! My ignorance made me think it may be a bug ! but it is not At All.
Scriptable objects are assets and by default Unity will remove all unused assets while scene change, thus removing the all that has been stored in the middle scene, and bring a fresh new asset in the next scene.
I almost went back to singleton approach, but i found this comment!
public class Foobar : ScriptableObject
{
...
private void OnEnable()
{
hideFlags = HideFlags.DontUnloadUnusedAsset;
}
...
}
Works Like a charm !