How do I make a Quit To Menu script?

I am making a drag and drop game for a school project, and the script I use was taken from the Unity tutorial How to Make a Menu, here is the script:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class QuitToMenu : MonoBehaviour
{

    public void LoadByIndex(int sceneIndex)
    {
        SceneManager.LoadScene(0);
    }
}

Whenever I press the Start button on my Menu then go to the game scene and click the Quit To Menu button it does not quit for me. It does not give me any errors using the script, so I do not know why it isn’t working.

Firstly, set the value of the scene you are loading to ‘sceneindex’ instead of 0 to make things easier.
Now when you want to load a scene, use LoadByIndex().

 public void LoadByIndex(int sceneIndex)
 {
     SceneManager.LoadScene(sceneindex);
 }

Now make sure that the menu scene has the index of ‘0’ before loading.


If done correctly, calling LoadByIndex(0) will load the menu scene.

Otherwise, double check your ‘quit to menu’ button and make sure that you are calling the LoadByIndex funtion, and that the value you are calling is ‘0’.

Let me know if this works for you!