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 /
avatar image
0
Question by DKitch9 · Dec 09, 2016 at 06:19 AM · buttonunityeditorinspectorrestart game

Restart button not functioning properly

Hello! I am trying to create a button that pops up once my game is over, to allow players to restart the game. The way I have it set now, the button pops up three seconds after the game is over. The problem is that you can click the button all you want, but nothing happens. And the strange part is that in the Unity editor, I have the button hidden because I obviously don't want it appearing while the game is being played but if check the box and make it visible at all times...it works properly and restarts the game! Any ideas on why that is or what I can do to make it work properly once the gameplay is over? Thanks in advance!

Comment
Add comment · Show 6
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 edcx · Dec 09, 2016 at 07:12 PM 0
Share

Can you tell us how you handle the game over and the restart? Looking at the code might help.

avatar image DKitch9 edcx · Dec 09, 2016 at 11:47 PM 0
Share

Yes, I guess I should have done that to begin with haha my apologies!

So this is how it's handled, while boxes are spawning, once an incorrect box is matched game over is called with "break". Then a game over text is displayed and the player is given the chance to restart the game with the restart button. And as I previously stated, the restart function on the button only works if it is set in the editor to be visible the entire time.

Calling Game Over:

 while (true)
         {
             yield return new WaitForSeconds(Random.Range(1.0f, 2.0f));
             int i = Random.Range(0, boxList.Length);
             Instantiate(boxList[i], transform.position, Quaternion.identity);
             yield return new WaitForSeconds(Random.Range(1.0f, 2.0f));
 
             if (gameOver)
             {
                 break;
             }
         } 

                 yield return new WaitForSeconds(2.0f);
                 gameOverText.SetActive(true);
                 yield return new WaitForSeconds(1.0f);
                 restartButton.SetActive(true);
     }
 
     public void GameOver()
     {
         gameOver = true;
     }

Calling Restart:

   public void RestartGame()
     {
         Scene$$anonymous$$anager.LoadScene("$$anonymous$$ain");
     }
avatar image edcx DKitch9 · Dec 10, 2016 at 09:51 AM 0
Share

Your logic works fine. I have tested it and it works as expected. Could it be that you are removing this script after the game is over or perhaps destroying the object that this script belongs to?

Show more comments
avatar image atifbutt · Dec 09, 2016 at 07:26 PM 0
Share

Time.timeScale is the variable which decides the time of execution of game when we set it to 0 everything stops execution so never set 0 you can set 0.00001f ins$$anonymous$$d

avatar image DKitch9 atifbutt · Dec 09, 2016 at 11:57 PM 0
Share

It sounds like calling Time.TimeScale more or less pauses or can slow down my game, which I'm not trying to do either of because the game is over. So the player can either go back to the main menu or restart the game from the beginning. Correct me if I'm wrong please! That's just how I'm interpreting the TimeScale function.

4 Replies

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

Answer by AurimasBlazulionis · Dec 10, 2016 at 08:09 PM

You can try changing while (true) to while (!gameOver). Also remove the gameOver check. Also check if any errors are called and check if all UI game objects are attached.

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 DKitch9 · Dec 11, 2016 at 06:03 AM 0
Share

That ended up working, thank you very much!!

avatar image
0

Answer by atifbutt · Dec 09, 2016 at 10:51 AM

Are you Setting TimeScale to 0 whenever game is over? If you are setting timeScale then after click functionality start you have to reset timeScale to 1

Comment
Add comment · Show 2 · 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 DKitch9 · Dec 09, 2016 at 03:17 PM 0
Share

I'm using 'break' to stop the gameplay...Should I try using the timescale function ins$$anonymous$$d?

avatar image atifbutt · Dec 09, 2016 at 07:24 PM 0
Share

Time.timeScale is set to 0 first and then after everytine when game start its set to 1 again

avatar image
0

Answer by TinoClemente · Dec 12, 2016 at 07:50 AM

If you have unchecked the box of your restart button in the editor it will not appear in the game, or it will not work fine. I recommend to use this code to make a canvas apear or dissapear:

 using UnityEngine;
 using System.Collections;
 
 public class Restart : MonoBehaviour {
   
     public Canvas restartButton;

     void Awake () {
         restartButton.enabled = false; //restart button is invisible at the begining
     }

     public void GameOver() {
             restartButton.enabled=true; //When the game is over, restart button appears
         }
     }
 }
 

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
avatar image
0

Answer by Coleclaw199 · Dec 11, 2016 at 08:59 AM

Do you have an eventsystem?

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

9 People are following this question.

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

Related Questions

Inspector scripting, adding lists, checkboxes and buttons [With Image] 0 Answers

Set icon of EditorWindow 2 Answers

Cannot add button object to Script in Inspector 2 Answers

Inspector button for custom class 1 Answer

Creating a Restart Button 3 Answers


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