- Home /
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.
Did you hook up your button's onclick event to that function?
Answer by Loui_Studios · Feb 03, 2017 at 10:02 PM
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!
Sorry, but the script does not seem to work. Whenever i replace the sceneIndex
to 0
it gives me errors. Sorry again, but I forgot to mention that I use Unity 5.3.3. This may be why it gives me errors.
Your answer
Follow this Question
Related Questions
Touch a box collider, quit the game. (2D) 1 Answer
Menu not working properly 1 Answer
Assets > Create > C# Script missing? 1 Answer
Pause menu not working 1 Answer
need help with armor customization 1 Answer