- Home /
How to return to Main Menu scene via buttons on the Gameover scene?
Hello Unity Answers,
I am trying to return to the Mainmenu scene once the game has finished. I have created a Mainmenu & gameover scene.
The gameover scene is made up of UI text which I have added a Button script to make them clickable buttons. I want the player to click PLAY AGAIN & EXIT to return to the main menu or exit the game.
All help and suggestions welcome.
Gameover scene
Main menu scene
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Gameover : MonoBehaviour {
private int count;
public Text countText;
public Button PlayAgainText;
public Button exitText;
public void UIActions (string loadActions)
{
if (loadActions == "Play Again")
{
Application.LoadLevel ("3");
}
}
void Start ()
{
DontDestroyOnLoad (gameObject);
count = 0;
SetCountText ();
PlayAgainText = PlayAgainText;
exitText = exitText;
if (GUI.Button (new Rect (20, 100, 100, 30), "1976"))
{
Destroy (this.gameObject);
count = count = 0;
SetCountText ();
}
}
void SetCountText()
{
countText.text = "Score: " + ValuesHolder.answersCount.ToString();
ValuesHolder.answersCount++;
}
public void StartLevel()
{
Application.LoadLevel (3);
}
public void ExitGame()
{
Application.Quit ();
}
}
Answer by nightbane30 · Apr 19, 2015 at 02:13 AM
You'll want to create a public method with an Application.LoadLevel in it, then assign it in the editor to your button. How to do this:
//make sure you have some kind of public method in your script like this
public void LoadMainMenu(){
Application.LoadLevel("NAME-OF-MAINMENU-SCENE-REPLACES-THIS-TEXT");
}
Next, go to the button (I'm assuming the one that says Play Again) and click on it. View it in the inspector and go to the component called "Button (Script)". From there, near the bottom of the component, click the small + icon in the box that's titled "On Click()". Then, drag and drop your gameobject with the script to load the main menu attached to it into the empty field on the bottom-left. After that, click on the button diagonally right of that titled "No Function". Navigate to the dropdown for the name of your script, and from there choose the name of the public method you created (In this case the one I wrote is called "LoadMainMenu()"). Then, hit play and it should be all good to go! Sorry if some of that was unclear. I can try and take annotated screenshots if you still need help with the problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613193450im_/https://answers.unity.com/themes/thub/images/avi.jpg)