- Home /
Question by
dar0001 · Oct 14, 2014 at 09:54 AM ·
c#scenedontdestroyonload
Scene changing
How would i go about switching back and forth between scenes and using the DontDestroyOnLoad function without creating a duplicate player gameobject.
Comment
Answer by voncarp · Oct 14, 2014 at 10:12 AM
This will stop duplicates.
using UnityEngine;
using System.Collections;
public class DontDestroyScript : MonoBehaviour {
public static bool created = false;
void Awake() {
if (!created) {
DontDestroyOnLoad(this.gameObject);
created = true;
}
else {
Destroy(this.gameObject);
}
}
}
will this also work for collectibles if it has been destroyed before?
Your answer
Follow this Question
Related Questions
Click handling at world scene level 1 Answer
C# Unity 3d Don't Destroy Class On Load 1 Answer
Multiple Cars not working 1 Answer
How to save a Scene during a game? 1 Answer
How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer