- Home /
DontDestroyOnLoad duplicate prevention code is deleting the original player
Still working on a 2D overhead game. So now I realized that the script I am using for DontDestroyOnLoad is deleting the original player that I am using.
DontDestroyOnLoad(transform.gameObject);
if (GameObject.Find(gameObject.name)
&& GameObject.Find(gameObject.name) != this.gameObject)
{
Destroy(GameObject.Find(gameObject.name));
}
This is the code that I am currently using to delete duplicates, but when loading back to the original scene, it deletes the original and messes up the whole rest of the code, such as the start points I am using.
Does anyone know how to fix this?
Answer by GamitusLabs · Nov 28, 2018 at 10:38 PM
This is how to make it so your object is the only instance and is preserved between scenes in Unity, this is similar to the 'singleton' design pattern.
public static GameManager instance = null;
void Start ()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
Destroy(gameObject);
}
Also, where does Game$$anonymous$$anager come from? It's shooting me an error.
$$anonymous$$y apologies,
Game$$anonymous$$anager is the class this is encapsulated in. So...
public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public static Game$$anonymous$$anager instance = null;
void Start ()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
Destroy(gameObject);
}
}
This method can be used on any class, just make sure that you truly only want one of them.
Answer by ecv80 · Nov 28, 2018 at 10:26 PM
I'm not very sure about your conditional there. GameObject.Find() returns a GameObject or null. GameObject overloads bool (ain't C# weird) to return true if not null. So there's nothing wrong with that usage... but I guess that's not the comparison you intended. You are saying: "If this game object exists(true/false) and this (exact same) game object exists(true/false), compare true/false (which is the result of true/false && true/false) to this.gameObject(true/false).
Now let's examine the case where your gameObject exists and your this.gameObject exists: (true && true) [=true] != true. This will always be false, so your conditioned code won't run, nothing will be destroyed.
Now the case where your gameObject exists and your this.gameObject (unlikely, I presume, but not impossible) doesn't: (true && true) [=true] != false. This will always be true, so your conditioned code will run, and gameObject will be destroyed.
Now the case where your gameObject doesn't exist and your this.gameObject does exist: (false && false) [=false] != true. This will always be true, so your conditioned code will run, and gameObject will be destroyed. Note that in this case, the first part of the condition will only run as far as checking whether the "first" gameObject exists, ignoring the "second" one, to then check the result against the this.gameObject.
Now the case where your gameObject doesn't exist and your this.gameObject doesn't either: (false && false) [=false] != false. This will always be false, so your conditioned code won't run. Same applies regarding the first check in the first part of the condition.
So as far as I can tell, your code isn't behaving as you intended. You would need to find different ways of checking if two game objects are clones or whatnot. You could compare their names, or hopefully something more reliable, which unfortunately I do not know.
Your answer
Follow this Question
Related Questions
When die - reset score from the last scene 2 Answers
Best way to deal with DontDestroyOnLoad when returning back to the same scene? 1 Answer
How to duplicate several layers in a tilemap?,How to duplicate layered tilemaps? 0 Answers
NPC Party Member Direction 1 Answer
how to load a different save position in different scene? 1 Answer