Saving and loading a GameObject built in-game

Hi, I’m trying to make a game with a gameplay style similar to Kerbal Space Program, where you first build your vehicle in game and then drive it in a second scene. However I’m not too sure on how to save the contructed vehicle and also load the it into the second scene.

Currently I’ve got 2 scenes : Construct and Main.
Constuct is loaded first and is where you build the vehicle. It basically consists of a bunch of GUI buttons that instantiate a component prefab that can be added to a vehicle base which is also a prefab. The components are added to the base as childs. Once the vehicle is finished a start button loads the Main game scene where you can pilot it against enemies and such. Once you finish an objective or your vehicle gets destroyed you return to the Contruct scene where you can modify or add more components and so on.

A quick example of a vehicle:

alt text

The problem I have now is how to load the vechile into the Main game scene, and also saving the vehicle so I can load it again.

So far I’ve tried putting DontDestroyOnLoad on the vechile but any damage taken is passed back to the first scene, also if the whole ship is destoryed its lost forever unless I create a duplicate first but that just feels wrong. Also it doesn’t address the saving issue.

I’m thinking that there must be some way of saving the created vehicle gameobject. It would pretty much have to create a seperate text file or XML file for saving so I’ve looked at other similar questions about saving with PlayerPrefs or XML but they all deal with saving and passing parameters such as positions rather than the actual gameobject itself. Currently I’m thinking the game would work by instantiating the saved vehicle on awake of the Main scene but I’m just not sure how to get the required info such as the different prefabs on the ship.

Considering that Kerbal Space Program is also made using Unity and it has pretty much the exact gameplay style that I’m aiming for I know it can be done, but just don’t know yet since I’m still relatively new to Unity. If anyone knows how to implement this your help would be greatly appreciated. Thanks

You can serialize whatever you want to something like JSON or XML (using any of the freely available libraries you will find from a basic google search.) You would create a database that exists separately from the unity save game state such that you can recall from it any of the information you store there. There are a couple of efforts at full serialization using introspection that might be worth your time as well.

Try Runtime Serialization for Unity for easy serialization of custom c# objects as well as Unity Objects like GameObject, MonoBehaviours, Textures, Prefabs etc. As a matter of fact, you can even use it for Scene Serialization. For more info about supported list, please check this 2.