- Home /
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!
Can you tell us how you handle the game over and the restart? Looking at the code might help.
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");
}
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?
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
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.
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.
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
I'm using 'break' to stop the gameplay...Should I try using the timescale function ins$$anonymous$$d?
Time.timeScale is set to 0 first and then after everytine when game start its set to 1 again
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
}
}
}
Your answer
Follow this Question
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