I’m very new to unity but I’m working on a function that needs to load another scene and then execute a function in it.
Code looks like this :
public void Respawn()
{
SceneManager.LoadScene("MainScene");
MenuManager.instance.LoadGame();
}
The problem is that after I load the new scene the second line of code isn’t executed.
The script I’m using is a singleton used for both the scene I’m calling the Respawn function from and the loaded scene.
Instead of calling LoadGame from Respawn() make your MenuManager register a method to the event SceneManager.sceneLoaded. This gets called right after a scene loaded so you can check in your MenuManager method which scene was loaded and call LoadGame accordingly.
Example:
//Inside MenuManager class
void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
//Check which scene was loaded using the Scene object and call LoadGame
}