Search Unity

Disable don’tdestroyonload object

Discussion in 'Scripting' started by max3241, Sep 15, 2020.

  1. max3241

    max3241

    Joined:
    Jul 12, 2016
    Posts:
    37
    Hello guys,
    So I need help with something. In my home menu, I made a don’tdestroyonload object. I was wondering when I start playing my game in another scene, is it possible to disable the don’tdestroyonload object by calling it with findobjectwithtag, so that I don’t want it to appear in my game scene? If so, how do I do it please.
     
  2. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    Why would you disable it when you have told it not to be destroyed (ie, keep it persistent over scenes?) Can you elaborate a bit more on this. It would be a better idea to keep a reference to it in another script and then manage it from there.
     
  3. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Code (CSharp):
    1. GameObject.SetActive(false);
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    can also Destroy() the gameobject, then it wont appear in new scene.
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Get a reference to the object when the other scene loads (
    FindWithTag
    or whatever else), and
    Destroy
    it like usual.

    Though, as it was already mentioned, it's a bit unusual that you want to destroy an object that's supposed to persist between scenes when a scene loads.

    What does the object do? Does it even need to be marked as
    DontDestroyOnLoad
    ?
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just make the GameObject a child of any other GameObject in the scene which you have not called DontDestroyOnLoad. It will then be destroyed on scene change, and you won't have to destroy it manually. If you change your mind again, just make it not a child again.

    DontDestroyOnLoad only works when the object is in the root of the scene.
     
    HCrowley, Saitilu, PHL1 and 3 others like this.