Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by bustyLaserCannon · Dec 12, 2013 at 11:18 PM · guigame over

Creating a gameover scene and the point just before it

So I'm making my first Unity game and it's going really well, it's a 2d roguelike game and I know when I'd like game over to occur.

Right now, it just restarts the game via

 Application.LoadLevel(Application.loadedLevel);

I'd like to create a gameover scene, but I haven't done much GUI other than displaying a score, but I'd like the scene to basically be 'Game Over' laid over across my scene, along with a button to restart.

How exactly would I go about doing this?

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
4

Answer by Vandarthul · Dec 13, 2013 at 03:06 AM

Lets just assume that your game's scene's name is level1 and gameover scene's name is gameOver. First of all you will have to change your code to display GameOver screen as

 Application.LoadLevel("gameOver");

Now you will have to design your Game Over scene. Since this is a simple scene that you've asked, I would go prepare a Game Over screen via a painting program(photoshop, paint.net) and googling "game over screens" would give you a quick idea how to make one. Generally its "Game Over" text over a black background.

Anyway when your Game Over screen is ready drag it to the unity. Now create a script and attach it to the Main Camera. Open it and;

 public Texture gameOverTexture;
 
     void OnGUI()
     {
         GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),gameOverTexture);
         if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2, 150, 25),"Don't try again, you are going to lose it anyway, because you are a LOOSER!")) 
         {
             Application.LoadLevel("level1");
         }
         if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2 + 25, 150, 25),"Run away, you coward! That's the only thing that you can do here.")) 
         {
             Application.Quit();
         }
     }

Now attach your Game Over screen to gameOverTexture in Editor and press play, this should be pretty much it. Well, you may need to change messages over the buttons here :)

In case you don't know what's going on there, read this: Now lets go through the code.

 Rect(left,top,width,height);

Rect takes 4 parameters here, first 2 being top and left which is simply "how much pixel you want to add to your object starting from left and top" So when you type 10, 10 there your object will start occuring from left, after 10 pixels and from top after 10 pixels. Now the other 2 parameter says width and height this is the width and height of your object that you want to create. Pretty much self-explanatory.

 GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),gameOverTexture);

So on your GUI.DrawTexture code, you see Screen.width and Screen.height and those are the values of your screen's width and screen's height. Considering your game will be played in different resolutions, those little variables there automatically taking the computer's resolution and making your objects appear corretly. So either you open it at 800*600 or 1920*1080 you will get your Game Over screen correctly drawed(make sure your Game Over Screen's resolution is high enough to cover deformations over low-res screens. Lastly we add our gameOverTexture because our GUI.DrawTexture wants us so :)

 if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2, 150, 25),"Don't try again, you are going to lose it anyway, because you are a LOOSER!"))

When we add GUI.Button using if statement, we simply say "draw this button and whenever someone clicks to it ..." here, we say start level1. This piece of code doesn't have anything different from what I've explained, except Screen.width / 2 and Screen.height / 2. Yes, we can do mathematical operations using Screen.width and Screen.height methods. So Screen.width / 2 will start from in the middle of the screen vertically, and Screen.height / 2 starts from in the middle of the screen horizontally. When looking at the message, -wow its a long message- it asks for GUIContent. Maybe you want to create a little image just before your text inside your button, you can create it with GUIContent(haven't tried but should happen).

  if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2 + 25, 150, 25),"Run away, you coward! That's the only thing that you can do here.")) 

I know it's all the same except Screen.height / 2 + 25 part. Now that's an important part. So we want this button to appear right next to our previous button. Therefore we draw this button after 25 pixels of our previous button horizontally. Because our previous button's height is 25 pixels. I believe you got the idea.

What if you don't want to use default unity styling here?

Now you got your unfriendly buttons insulting your player, but what if you want to change the button's appearing, or font? Say hello to our dear friend GUISkin. Firstly you will have to create a GUISkin on your Editor by clicking Create - GUI Skin under your Project. This basically allows you to change every piece of GUI material in your project. After creating, go through your code and add a public variable:

 public GUISkin myGUISkin;

And on top of your ONGUI:

 void OnGUI()
 {
      GUI.skin = myGUISkin;
      GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),gameOverTexture);
         if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2, 150, 25),"Don't try again, you are going to lose it anyway, because you are a LOOSER!")) 
         {
         .
         . //Rest of the code here
         .

Now, select our newly-created GUISkin and attach it to the public variable that we've just created in the code. After that, click to GUISkin again and Click Button-Normal and change Text Color. When you start the screen, you will see that your texts' color will change accordingly. So you can change the background of your buttons as you want. The rest, Hover Active Focused are states of your buttons. For example if you bring your cursor over your button its actually hovering your cursor over button. So if you want to create funky GUI, you can also change the Hover's background and achieve it.

That's pretty much it. I hope you enjoy my explanations and I strongly suggest you to search for your problem first. For example googling "how to make a button in unity" "how to customise a button in unity" would do the trick. There are ridiculously number of tutorials out there just for unity covering mostly every aspect of it. And you can be sure that general questions/problems like this is being asked before you. And its a lot faster than opening a new question and waiting for an answer. Also and lastly you should use **THIS**(quick tip, CTRL+F to search is way faster than that little search box over there) website as it's your main source of unity scripting. Its like uber magic handbook of unity scripting, and you will get what you want mostly. In case of your problem, you could simply look at **HERE**. Anyway that's it for me, good luck with your game :)

Note: I didn't quite try the scripts that I've provided, but you got the idea and you are now capable of fixing funny stuff if happening.

Comment
Add comment · Show 3 · 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 YoungDeveloper · Dec 13, 2013 at 03:07 AM 0
Share
  • For detailed explanation!

avatar image bustyLaserCannon · Dec 13, 2013 at 09:47 AM 0
Share

Incredible explanation. Thanks very much, will try when I get home.

avatar image ocimum · Aug 03, 2017 at 01:42 PM 0
Share

From Unity 5.0 you should use the Scene$$anonymous$$anager because the other methods are deprecated. http://answers.unity3d.com/answers/1302634/view.html

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

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

Game Over 2D GUI position 0 Answers

The Game Over creating with audio? 1 Answer

Display Time At Game Over Scene 1 Answer

Game Over Screen Problem 3 Answers

GameOver Scene 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