Reputation: 152
I'm reloading the scene after a user clicks on retry. The Update function does not work after reloading the scene.
I searched the web and found DontDestroyOnLoad()
, but I don't know how can I use it with a function.
Used
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
to reload the scene.
Is this a bug from the unity side or i need to do something else before reloading the scene.
Upvotes: 0
Views: 709
Reputation: 86
You can make the object witch the script is in not be destroyed when you load a scene using DontDestroyOnLoad()
. But when you load the same scene there will be already the same object in that scene, so you will have to destroy it.
private static GameObject goInstance;
void Awake(){
if (goInstance == null) {
DontDestroyOnLoad(gameObject);
goInstance = gameObject;
} else {
//destroy duplicate
Object.Destroy(gameObject);
}
}
Upvotes: 1