Business Cactus
Business Cactus

Reputation: 15

How to stop GameObject duplications when reloading a scene?

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.

Upvotes: 0

Views: 434

Answers (1)

Igor Belikov
Igor Belikov

Reputation: 272

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);
  }
}

Upvotes: 0

Related Questions