Scripts not reloaded after SceneManager.LoadScene()

Hi!

I’ve been struggling with a following issue:

I want to have “Main Menu” button, clicking on which a Player goes back to main menu (different scene) and all his data on the current scene is destroyed. I have a bunch of Controller scripts, that are made in Singleton pattern, and I want them to be deleted with the LoadScene, but they just stay there and make annoying collisions with newly created ones (when Player starts new game).
Here is what happens, when Player clicks “Main Menu” button:

public void MainMenu()
    {
        Debug.Log("Loading Main Menu");
        SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    }

Does anybody have any idea, what can be the cause of this problem? I’ve been sitting around it for quite a while and can’t figure out, what’s wrong :frowning:

,Hi!

I’ve been struggling with a following issue:

I want to have “Main Menu” button, clicking on which a Player goes back to main menu (different scene) and all his data on the current scene is destroyed. I have a bunch of Controller scripts, that are made in Singleton pattern, and I want them to be deleted with the LoadScene, but they just stay there and make annoying collisions with newly created ones (when Player starts new game).
Here is what happens, when Player clicks “Main Menu” button:

public void MainMenu()
    {
        Debug.Log("Loading Main Menu");
        SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    }

Does anybody have any idea, what can be the cause of this problem? I’ve been sitting around it for quite a while and can’t figure out, what’s wrong :frowning:

Hi there, How do you organize your Singleton Pattern?

Do you have your scripts on different objects or just one?


You can create for each script a “self-destruct” method, that is, leave its instance as null and destroy the object where this script is located. and another that is an empty delegate that is in charge of calling the “self destruct” function of all the scripts


public void DestroyMyself()
    {
        Instance = null;
        Destroy(gameObject);
    }

You could set up a script called “PlayerDelegate” to take care of having this Delegate void


public class ExamplePlayerDelegate : MonoBehaviour
{
    public delegate void OnMainMenuLoad();
    public event OnMainMenuLoad onMainMenuLoad;

    public void OnMenuLoadExecute()
    {
        onMainMenuLoad.Invoke();
    }
}

And within each Singleton, you can “Load” that self-destruct function in the PlayerDelegate script

public void Start()
    {
        FindObjectOfType<ExamplePlayerDelegate>().onMainMenuLoad += DestroyMyself;
    }

And in your MainMenu void, the only thing you do is look for the “PlayerDelegate” and execute that void


public void MainMenu()
    {
        Debug.Log("Loading Main Menu");
        ExamplePlayerDelegate playerDelegate = FindObjectOfType<ExamplePlayerDelegate>();
        playerDelegate.OnMenuLoadExecute();
        SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    }

Hope it helps!