Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by vaders_girl · Mar 09, 2016 at 02:05 AM · c#buttonscripting beginnerrestart

Restart option on game over?

Hey,

I am making just a simple 2D vertical shooter in Unity, using c#. I have had a look online and can't seem to find exactly what I am looking for.. All I want is to have a GUI text button to restart the game once you are killed by an enemy. I have made the Restart text, added the button component and have hidden it from the scene (only want it to show up when the game is over) After looking online I found that this is a simple way of doing it

 void OnGUI () {
     if (GUILayout.Button ("Restart")) {
         Application.LoadLevel(Application.loadedLevel);
     }
 }


However that just brings up a restart button on the top left of my game, instead of setting it to my restart text.

Not sure what code I need to be putting, adding to the text or where I need to be adding this code? Will it need to go under where I have the game over text popping up?

Any and all help is much appreciated. Thanks very much!!

Here is the part of my enemy script which contains the game over on collision with an enemy..

 void OnTriggerEnter2D (Collider2D other)
 {
     if (other.gameObject.tag == ("Player"))
     {
         playerController.gameOverText.gameObject.SetActive (true);
         gameObject.SetActive (false);
         other.gameObject.SetActive (false);
     }

     if (other.gameObject.tag == "Bullet")
         gameObject.SetActive (false);
 }

 void OnGUI () {
     if (GUILayout.Button ("Restart")) {
         Application.LoadLevel(Application.loadedLevel);
     }
 }

}

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
0

Answer by Zoogyburger · Mar 09, 2016 at 03:40 AM

If you want to do it in script, this should help you:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class GameOver : MonoBehaviour {
         void OnGUI()
         {
             const int buttonWidth = 120;
             const int buttonHeight = 60;
             
             if (
                 GUI.Button(
                 // Center in X, 1/3 of the height in Y
                 new Rect(
                 Screen.width / 2 - (buttonWidth / 2),
                 (1 * Screen.height / 3) - (buttonHeight / 2),
                 buttonWidth,
                 buttonHeight
                 ),
                 "Retry!"
                 )
                 )
             {
                 // Reload the level
                         SceneManager.LoadScene("Scene Name");
             }
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

131 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 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 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 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 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 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 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

How to add a restart to my 2D game? 0 Answers

How to enable and disable back game object for ui button after a few seconds? 1 Answer

How To input text from a button? 1 Answer

Changing a variable in another script on Raycast hit - C# 1 Answer

[SOLVED]Multiple enemies Wave Spawner 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