Saving and Loading GameObjects to another Scene in runtime

Hi,

i’m trying to make an application, where the user can build and alter a GameObject in one Scene, then save this gameObject and finally place it in another scene. I’m trying to make something like a design Game, but am struggling with the saving and loading of the objects.

So in one Scene the user creates a gameObject by instantiating a few prefabs as children of this gameObject, moves them around, gives them a rotation etc.
After this process of building, i want the user to be able to save this gameObject including all its children (their position relative to the parent, their rotation, all their components and scripts). In another scene i want the user to be able to load and instantiate this gameObject and position it in the room.
(This is supposed to be the part, where the user places his composition in a museum-like room).

So far the creating-part works fine. I’m struggling with the saving and loading. As far as i know, it is not possible to create/update prefabs in runtime, which was my first idea.

Do you have any ideas, how i could solve this? How could i save and load gameObjects in runtime in different scenes, that are build by the user?

I’m using Unity 2020.1.5f1.

Thank you!

You have to think about what that composition modifies or consists of and save that.
For example, which prefab, position, rotation and scale, parent relationship and content if the object contains a script with variable content.
When saving you store all that and upon loading reconstruct the hierarchy from that data, that’s how it’s done almost everywhere (even unity itself does it that way).
The complex part is handling unity-objects which, as user, you can only referene, so usually that is done by keeping a list with identifiers and references to those objects and then saving the identifier with the data. When reconstructing just retrieve the object from the list by identifier and continue. But that depends on how your objects are designed. You may also just create them on the fly if your setup allows that (new GameObject, attach script, set script data etc). Saving and restoring script data is somewhat generically possible with JSONUtility, but the rest you have to design yourself or find an asset store solution where someone else has already done that (i think there are some saving solutions available). If you don’t wont to spend money then you have to think about designing the data structure your setup requires to map the information back and forth. if it’s only placing prefabs then it shouldn’t be too complicated

Whenever you have to save and load data ScriptableObject is your friend.
Just search on Youtube

Thank You!

In the meantime i created a little workaround.
After the user made the Object, i move it with the MoveGameObjectToScene() to Scene i want it to be in, right before i load the next Scene. There i call the DontDestroyOnLoad() on the object and push it into a list. Whenever i enter another Scene i deactivate all Objects in the List. Whenever i enter the “museum-like show room”-Scene i activate all objects in the list. This is done with the SetActive() function.
So basically once an object is made by the user, i never destroy it, but rather deactivate and activate it depending on the scene the user is in.
Do you think this will work for a couple of objects or is this heavier on resources than instantiating saved objects?
Do deactivated Objects use computing power at all?

I hope this kind of weird workaround doesnt get me in problems. It saves me from saving all the rotational-, position-, sound-data and so on of each single Object with all its children in scripts. That would be a painful process, i guess.

Thank you so much for your help!

6741043--776845--upload_2021-1-19_19-3-0.png

Sure, if you don’t need to save the objects in a way where they can be loaded after the game has been closed and opened again, then this is much simpler. It’s unlikely you’ll see performance issues due to this unless you have a very high number of these objects.

1 Like