How to stop GameObject duplications when reloading a scene?

238 Views Asked by At

I'm making a game where the player can switch back and forth between levels. I use "DontDestroyOnLoad" to bring my player between them, but whenever I reload a scene, it duplicates the player, as well as everything else I want to keep loaded. Does anyone know any easy fix to this? I'm new to programming so keep it simple please.

1

There are 1 best solutions below

2
Igor Belikov On

probably you need to write something like this:

public static <TypeName> instance = null;
void Awake() {
  if (instance != null) {
    Destroy(gameObject);
  } else {
    instance = this;
    DontDestroyOnLoad(gameObject);
  }
}