- Home /
Serialize, deserialize and keep references alive
Hello.
I am working on a software, that needs to serialize and deserialize a loot of objects. Those objects will be removed when serialized, and later they will be added again by deserializing the data. I now want to reestablish all references pointing on the object when it is recreated.
Given the example with object A that holds a reference on object B:
When I serialize and destroy B, the reference will be broken.
Some time later, I recreate B and want the reference from A to B to be recreated.
Can someone tell me, how I can reestablish the reference from object A to object B?
Is there a fast way to get all references in the scene pointing on a certain object?
I'm using C# and Unity free.
Thanks
Greetings
Chillersanim
Answer by MakeCodeNow · May 25, 2014 at 01:43 AM
The simples solution would be to add a new component which creates a GUID for each object. When you save out the object references, save out the GUID of the referenced object. When you load the object reference, try to find an object with your component whose GUID matches and fixup the reference. Done!
So I should store the GUID when serializing the object and later when I recreate the object, I should give it the GUID?
And then it already has all references?
Cool.
Greetings
Chillersanim
Yes. If you use a new component to create the GUID, Unity will store that for you. All you'll need to do is make that component, and then do the reference serialization and fixup post deserialization.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145816im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity still references file moved to different folder 1 Answer
Custom assets give Missing (Mono Script) 0 Answers