DontDestroyOnLoad duplication despite having code that should stop it
I have an object with DoNotDestroyOnLoad and I have code in place that should stop it from duplicating each time I enter the scene where it was instantiated. The weird thing is that only one other duplicate will show up in the editor, but the effects of having more than that will occur.
Here is the code I'm using that pertains to this issue:
public class gameManagerScript : MonoBehaviour {
public static gameManagerScript Instance;
// Use this for initialization
void Start () {
if (Instance) {
DestroyImmediate (gameObject);
} else {
DontDestroyOnLoad (gameObject);
Instance = this;
}
}
}
I've also tried using FindGameObjectsWithTag and checking to see if the length is more than one, but nothing I try is working.
Answer by Nomppie · Mar 21, 2017 at 10:30 PM
I fixed it. The problem wasn't related to how I was doing it. It was because I left the stuff I wanted the script to do on the outside of the if statement.
Answer by cjdev · Mar 21, 2017 at 09:08 PM
Try something like this instead:
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(this);
}
else if(this != Instance) {
Destroy(this.gameObject);
}
That doesn't work either; all of the problems are still there :/
Your answer
