How to call a public function automatically when I return to the Main Menu from any levels?

Hi guys,

so I have a menu scene where I can switch between 2 pages of that menu. First page of the menu is some info about the game and exit and the second page of the menu is summary of levels.

Ok, now I want to start the game. First page of the menu shows up. Then I want to choose a level to play so I go to the second page of the menu. In there I click on any level. The level loades and I play. But when I get killed I want to return to the second page of the menu.

I switche between menu pages using buttons and public functions like: “public void ShowPageOfLevels” “public void MenuFirstPage”. I have buttons using OnClik() that triggers this functions. But how to call the function when I return from any other scene?? Or in general how to return to the second page of the menu??

if the menu is in two scenes, just load directly the second one.
If it’s in one scene, and you need to call the function to go to level page, just create a gameobject with a certain script on it, and call DontDestroyOnLoad with the object in parameter.

there is the script i mentioned before :

using UnityEngine;

public class loadWithLevelPage : MonoBehavior
void OnLevelWasLoaded()

You can then use OnLevelWasLoaded like this:

void OnLevelWasLoaded(int level) {
    if (level == 0) { //Assuming that the Main scene is the first scene under the list in File->Build Settings
        //Call the function here:


I finaly figured out a different way how to solve my problem. I created a private static bool. Static variables are instances of the class and are not reseted when loading a different scene.

My script now looks something like:

private static bool isInMenuMoreThanOnce;

void Start()
		ShowPageOfLevels ();     // Calls the function that displays the page with levels

public void ShowPageOfLevels ()
	if (!isInMenuMoreThanOnce)
		isInMenuMoreThanOnce = true;

        // some other code of the function

Thanks guys for your help.