- Home /
DontDestroyOnLoad
A friend told me about a blog post stating that DontDestroyOnLoad is implemented not by persisting the object between scenes but actually doing a deep copy into a new instance in the new scene, and then deleting the original object. Is that true? If it is, it would have ramifications about fragmenting memory across scenes, especially singleton classes that hold a lot of data. Can someone from Unity please confirm?
$$anonymous$$y understanding is that when a new scene is loaded all objects are deleted and using DontDestroyOnLoad simply marks the object not to be deleted when this happens. If Unity was doing a deep copy then I would expect private variables and none serializable objects would become corrupted/null between scenes and this has never happened for me.
Answer by Kiwasi · Aug 27, 2014 at 07:42 PM
Short answer is No. Unity simply tags the objects not to be destroyed, then leaves it alone when destroying all GameObjects in the scene.
Unity is highly optimised for most things. Its far easier simply not to call destroy on something then to copy it. Especially considering the inherent problems doing a deep copy. As I understand it Unity has no built in mechanism for a deep copy.
Your answer
Follow this Question
Related Questions
Is it costly to get a singleton instance each frame ? 1 Answer
Property vs Field. Which is faster? 1 Answer
Singleton vs AddComponent 3 Answers
What is a fragment? 1 Answer
Differentiate an overlapping collision from just touching 1 Answer