- Home /
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:
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
Answer by tingham · Feb 08, 2012 at 01:26 PM
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.
http://answers.unity3d.com/questions/8044/what-is-the-easiest-json-library-to-integrate-with.html
Transform/GameObject class were made non-serializable hence cannot be serialized directly. The OP would have to serialize pertinent data to reconstruct the object later but cannot serialize the object itself.
You're right and I should clarify my "whatever you want" to "the pertinent information." Let's see how the answer fairs when the poster gets back.
Thanks for the reply. I'm still looking into this though since I'm only working on this casually. Never the less I'll still check out JSON considering it won't hurt to know more. Anyway I'm somewhat suprised that this saving feature is so complicated to implement.
You can take a look at SerializerHelper, a ffree asset written by me that lets you save and load whole gameobjects, and has lots of documentation throughout the scripts so it's easy to understand and modify/write your own serialization system.
Answer by Voxel-Busters · Aug 09, 2015 at 05:04 AM
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 link.
Your answer
