• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RKM_91 · Apr 18, 2015 at 10:52 PM · gameover

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.

alt text

Gameover scene alt text

Main menu scene alt text

 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 ();
     }
 }
 
 
 
 
 
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RKM_91 · Apr 19, 2015 at 07:45 AM 0
Share

Thank you so much, very clear instructions. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

gameover script 2 Answers

How do I give an option to retry a level or return to a level selection menu opposed to just restarting the level? 1 Answer

Game Over Screen appears on launch.,Something with my script makes it so that everytime I run it a gameover happens 1 Answer

How to set a game-over scene when an object hits another? 0 Answers

How do I make Game Over text stay enabled for a few seconds before the Application Load function runs? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges