Search Unity

Problem using DontDestroyOnLoad

Discussion in 'Scripting' started by jsull1, Jul 12, 2018.

  1. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    I've used DontDestroyOnLoad before but it just won't work for this over object and I have no idea why. Sorry for the rough code, I'm just trying everything and haven't cleaned it up

    Code (CSharp):
    1. public GameObject ThisObjectInstance;
    2.     public GameObject ThisHereObject;
    3.     public string TagToSearchFor;
    4.  
    5.     void Awake ()
    6.         {
    7.         DontDestroyOnLoad (this);
    8.         DontDestroyOnLoad (this.gameObject);
    9.         DontDestroyOnLoad (ThisHereObject);
    10.         ThisObjectInstance = GameObject.FindGameObjectWithTag (TagToSearchFor);
    11.         if (ThisObjectInstance == null) {
    12.             this.transform.gameObject.tag = TagToSearchFor;
    13.             //ThisObjectInstance = this;
    14.             DontDestroyOnLoad (gameObject);
    15.             } else {
    16.                 Destroy (gameObject);
    17.             }
    18.         }
    19.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Well, DontDestroyOnLoad(gameObject) should be all that is needed...but you have a bunch of them there, so I'm not really sure what you tried and it's a bit throwing off.

    But you also don't mention what you want. Do you want the gameObject with that script to not be destroyed? Are you trying to target another object? Are you seeing any errors? Is the object always being destroyed?

    That code might always be destroying the object if it never finds the tagged object.
     
    jsull1 likes this.
  3. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Sorry yeah I tried it every way I could to get it to go into the DontDestroyOnLoad category. But even trying it without the rest of the script, just saying void Awake (){DontDestroyOnLoad(gameObject)} is not functioning. And the rest is just to add a tag so the duplicate will delete itself but I haven't even gotten to that problem haha
     
  4. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    This is what is currently on it now and its still not working
    Code (CSharp):
    1. void Awake ()
    2.         {DontDestroyOnLoad (this.gameObject);}
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm not sure what you're trying to do with this code either. What do you mean by "is not functioning?"

    Also, add a debug.log line to verify that you script is actually being run. All you really need for it to work is the below. Also I believe the object needs to be in the root of the scene. If it is not its parent will be destroyed on scene change, taking this object with it.

    Code (csharp):
    1. void Start()
    2. {
    3.     DontDestroyOnLoad(gameObject);
    4.     Debug.Log("Called DontDestroyOnLoad");
    5. }
     
    jsull1 likes this.
  6. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    What do you mean by the root of the scene?

    And I'm making a game where you're a dog and the objects in question are FireHydrants. If the player reaches one they get a point that is counted in the right hand corner. When you use the hydrant, the component on it that adds the score is deleted to keep players from scoring more than once at the same Hydrant. But when I reload the scene, the component comes back, so I'm looking to just keep the Hydrants in DontDestroyOnLoad rather than make another component that keeps track of which of the 20 hydrants have been used.

    Essentially, all I'm trying to do is have this object not destroyed on load
     
  7. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Okay I ran it with the debug.log and it printed it, so it is being called
     
  8. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Oh I figure it out! It didn't say it in the documents but the things you put in DontDestroyOnLoad cannot be children of other objects. I'm sorry for the confusion, I had no idea. But thanks for your help though!
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yeah, that's what he meant by "must be in the root".
     
    jsull1 likes this.
  10. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Oh my bad, I'm very new at this
     
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That's what root of the scene means :) No parents

    Glad you got it working!
     
    jsull1 likes this.
  12. Mudassir2541

    Mudassir2541

    Joined:
    Mar 19, 2022
    Posts:
    1
    hi everyone
    I'm making a game in unity. when I'm in the game mode they show ( DontDestroyOnLoad ) this error in my game mode because of error I can not destroys my object or if I lunch my object by spacebar key so the game mode stop.
    why
    please help me i'm beginner
     

    Attached Files:

  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Don't revive a 4 year old thread.
    Start your own post, be clear about what you're asking (You mention an error, but not what your error is). And honestly, I don't know what you're asking at all. But just start your own thread.