DontDestoryOnLoad HELP!

Hi,

I must be fundamentally not understanding DontDestroyOnLoad. I have a Splash Screen on my app followed by a Main Menu Scene followed by several menus.

Since all the menus share common objects, I called DontDestroyOnLoad on several of these objects in the Main Menu Scene- GUITexts, Lights, a Plane, a 3D character and what have you.

Its working fine as long as I keep going forward through the menus. Its even working fine while going back through the menus. But when I come back to the Main Menu, all the objects that I called DontDestroyOnLoad are duplicated.

How do I avoid this?

Any help would be appreciated.

Thanks

Yogi

test in the Awake() function if an object of that type already exists. If so just Destroy it again.

The Awake() method is only called once the object is created, so you don’t cause performance problems due to that with the longliving object instances

Thanks Dreamora,

If I understand you right, before calling DontDestroyOnLoad in an object’s Awake function, I have to check if that if an object of this type exists already.

I have several GUI Text objects on screen, so wouldn’t checking objects with same name be correct here.

Let me experiment anyway and see how it goes.

Thanks for the help.

should have better formulated it: check if more than 1 is present by FindObjectsOfType
if more than one is present, then destroy the one which the component is in

Thanks Dreamora,

I will try this now.

:slight_smile: