Optimal way to save minimal serialized scene status, at runtime

My usage case is this: a home scene with menus and submenus. After leaving it to a brand new scene, everything gets destroyed. When getting back to the home scene, everything is recreated. I wanted to return to the same submenu, thus I need to save its status some how.

The status isn’t all that complex in the end. There are a few (NGUI) panels and only one is active at a time. I just need, basically, to remember the last one. Basically. So this doesn’t need to be a complex scene serialization. I think. And it is in runtime, nothing to do with the editor!

I’m currently resolving this, using the gameobject’s names. But they need to be unique and I use a GameObject HardFind (I hate how unity answers can’t properly link to an answer!), which will search through all objects including disabled ones. So, that’s all far from optimal.

Any better ideas on how to approach the issue? Any better solutions to this approach?

Since unity doesn’t have simple built in functionality to save (serialize) a scene, what you need to do is save all the data that you need to reproduce the current state in persistent storage (in our case persistent means something that is kept between scenes, unless you want it to remain between games and then you’d need to store it in the disk).

The easiest way in unity to do that, is store all the data you need inside a script attached to a GameObject, and use DontDestroyOnLoad to make sure that game object persists between loading scenes.

Then whenever the scene is loaded, you need to reproduce the last state using the information in that GameObject. In your case you might need to keep in the GameObject which submenu was open, and maybe any values that the user set in the menus. Then when the scene is loaded, read which submenu should be displayed from the object, and display it. Make sure that the same object puts in default values (the home menu I guess) in it’s Start(), which will only be called the first time you load the menu, because in subsequent times it will not be re-created.