0
votes

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.

1
You haven't included enough information here to adequately diagnose the problem. DontDestroyOnLoad() takes a game object parameter. That game object (and all its components) will not be destroyed when LoadScene is called, but if the new scene also contains that game object, you will end up with a duplicate. - Draco18s no longer trusts SE

1 Answers

1
votes

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