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
public GameObject ThisObjectInstance;
public GameObject ThisHereObject;
public string TagToSearchFor;
void Awake ()
{
DontDestroyOnLoad (this);
DontDestroyOnLoad (this.gameObject);
DontDestroyOnLoad (ThisHereObject);
ThisObjectInstance = GameObject.FindGameObjectWithTag (TagToSearchFor);
if (ThisObjectInstance == null) {
this.transform.gameObject.tag = TagToSearchFor;
//ThisObjectInstance = this;
DontDestroyOnLoad (gameObject);
} else {
Destroy (gameObject);
}
}
}
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.
1 Like
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
This is what is currently on it now and its still not working
void Awake ()
{DontDestroyOnLoad (this.gameObject);}
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.
void Start()
{
DontDestroyOnLoad(gameObject);
Debug.Log("Called DontDestroyOnLoad");
}
1 Like
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
Okay I ran it with the debug.log and it printed it, so it is being called
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!
Yeah, that’s what he meant by “must be in the root”.
1 Like
Oh my bad, I’m very new at this
That’s what root of the scene means
No parents
Glad you got it working!
1 Like
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
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.