Hello!
I have one main GameObject that I used DontDestroyOnLoad(transform.gameObject); and I have it created in first scene and its there for all other scenes in the game.
But in order to test game levels separately I need to put that main game object in every scene that I am testing so that I don’t have to test from first scene every time. So I am afraid that I could forget to remove this game object from some of levels and then in that level I would have 2 main game objects instead of 1.
Is there some way of testing if there are two of the same game objects in one scene? So that I can trace a warning if I accidentally left main game object in level and now I have one created at start and one from this scene?
Thanks!
2 Answers
2Solution is implementing singleton pattern script on a desired game object in one scene test it save it as prefab, put prefab on other scenes, on load of each scene game object in scene will be replaced with old one.
There are 3 ways to do it and all are described here
I was looking how to save game in unity and I run into this video that also given me exact answer to question I made here :) http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading Anyway thanks for answer. Seem to be similar thing like in beginning of this video.
– EntertainmentForgeJust run GameObject.Find(“objectName”), if you get a result then run DestroyImmediate() in Awake().
Give it a unique name or a unique tag. Then you can use GameOject.Find() or GameObject.FindWithTag() to detect if it exists.
– robertbuUse the singleton pattern. Instead of destroying the extra GameObject log an error or warning. Someone may give you some code
– Kiwasi[http://unitypatterns.com/singletons/][1] [http://wiki.unity3d.com/index.php/Singleton][2] [1]: http://unitypatterns.com/singletons/ [2]: http://wiki.unity3d.com/index.php/Singleton
– robertbu