- Home /
how can serialize a gameobject
how can serialize a whole GameObject with his childrens and component with the running of a "void" and deserialize all with the running of another "void"?
something like that:
public GameObject save;
void OnSave()
{
here the commands to serialize the gamobject "save"
}
void OnLoad()
{
here the commands to deserialize or "load" the gameobject "save"
}
}
Answer by Nerull22 · Sep 02, 2016 at 03:10 PM
This is a can of worms kind of question. There really isn't a straight forward answer to this. I'll supply some links for you and hopefully that will be enough to get you started on finding the best way that will work for you. First is a series I came across the other day about serializing and deserializing in various ways.
https://www.youtube.com/playlist?list=PL5KbKbJ6Gf9_Rvhx1ye1iHxtLTsAaH7uD
That is the playlist, so I recommend watching that. It has some step by steps for simple saving and loading.
I also strongly recommend a Unite talk on Scriptable Objects. Scriptable Objects are super powerful and often overlooked and placed in unnecessary boxes that they well exceed.
https://www.youtube.com/watch?v=VBA1QCoEAX4
That's a great talk on the power of scriptable objects and some of the things that you can achieve.
To a more general answer to your original question, what I tend to do is serialize the game object down into data and when I want to reconstruct a game object that the data represents then I pass the deserialized data into a factory that will build the object back for me using the data. You can't create a game object from itself, it needs a beginning point. If you're unfamiliar with factories there is a fantastic series of design pattern videos by Derek Banas where he goes over the factory and abstract factory pattern in general.
https://www.youtube.com/watch?v=ub0DXaeV6hA
That should get you started along a pretty good path of information and then you just need to decide what combination of methods is going to fit best for your game and what you're trying to achieve.
Your answer
![](https://koobas.hobune.stream/wayback/20220612092023im_/https://answers.unity.com/themes/thub/images/avi.jpg)