Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Objects being duplicated with DontDestroyOnLoad()

Discussion in 'Editor & General Support' started by moproductions, Mar 29, 2012.

  1. moproductions

    moproductions

    Joined:
    Sep 28, 2010
    Posts:
    72
    I've got a class I call MCP (master control program) attached to a GameObject that I'm using to store things like game state and settings. It is part of the FrontEnd scene file hierarchy. Since it has to be shared throughout all of our levels I'm calling DontDestroyOnLoad() during MCP.Awake(). This is fine when leaving the front end and going into the various levels of our game because the MCP stays around. However, when I come back to the FrontEnd, there's now two copies of it, which is causing all sorts of conflicts. Is there a way to have an object be kept around but not duplicated when returning to the scene that it defined in?
    Thanks
    -Mo
     
  2. homeros

    homeros

    Joined:
    Dec 23, 2009
    Posts:
    121
    One way is to create that gameobject on a scene before frontend, which will only be loaded when the game is first started. Or you can try instantiating that object from prefab. That way you can check if the object is already instantiated, if it is you won't instantiate again.
     
  3. moproductions

    moproductions

    Joined:
    Sep 28, 2010
    Posts:
    72
    Thank you, Speed. That's actually something that had occured to me because we DO have a splash scene with all of the various logos and all that. But I can see this kind of thing happening in the future in other cases so I'm trying to nip it in the bud before crunch time.
     
  4. moproductions

    moproductions

    Joined:
    Sep 28, 2010
    Posts:
    72
    Solved. I keep a static instance around and in Awake() if they dont match then trash it and move on.