Better Than A Singleton

Hello, I would like to ask three questions on creating Persistent Objects

  • Should we place it at the first scene only or at every scene?
  • Can we prefab the Object Spawner, so that we can drop it at all appropriate scenes
    at hierarchy?
  • If we do, then variables assigned at runtime will remain the same across scenes?

Thanks in Advance!

You can/should put the spawner in every scene. The spawner will ensure the persistent objects are only spawned once. Don’t put the persistent objects themselves, however, in any scene, just the spawner. (Since FindObjectOfType searches the current scene before searching the objects in the DontDestroyOnLoad Scene, it will find the one in the current scene first and use it instead of the one in DontDestroyOnLoad).

You can prefab the spawner. It’s a good idea to do just that. Then you everything stays consistent across your scenes. Since the spawner itself uses a static bool to the knowledge that the spawner has spawned persists between scenes.

Variables on the items in the PersistentObjectsPrefab will persist, as the gameobjects they are on will persist between scenes.

Thank you very much!!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms