Question by
DubstepCheesecake · Jun 02, 2017 at 05:06 AM ·
unity 5buttontimer countdownrestart game
How to make a button appear with a condition?
Hi, I am currently trying to create a restart button for my game. It should appear when the time is up but somehow the button's not appearing when the time's duration is up.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Restart : MonoBehaviour
{
public GameObject button;
public int timeLeft = 60;
// Use this for initialization
void Start()
{
button.SetActive (false);
}
// Update is called once per frame
void Update()
{
if (timeLeft <= 0)
{
button.SetActive (true);
StopCoroutine("LoseTime");
}
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name); // loads current scene
}
IEnumerator LoseTime()
{
while (true)
{
yield return new WaitForSeconds(1);
timeLeft--;
}
}
}
Comment
Answer by nathanlink169 · Jun 02, 2017 at 12:11 PM
It does not appear like you are starting the LoseTime Coroutine anywhere. You should call StartCoroutine in the Start() function.
Hi nathanlink169 I've added the StartCoroutine("LoseTime"); in the void Start() and the button is still not appearing. D:
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine.UI;
public class Restart : $$anonymous$$onoBehaviour
{
public GameObject button;
public int timeLeft = 60;
// Use this for initialization
void Start()
{
StartCoroutine("LoseTime");
button.SetActive (false);
}
// Update is called once per frame
void Update()
{
if (timeLeft <= 0)
{
button.SetActive (true);
StopCoroutine("LoseTime");
}
}
public void RestartGame()
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name); // loads current scene
}
IEnumerator LoseTime()
{
while (true)
{
yield return new WaitForSeconds(1);
timeLeft--;
}
}
}
Your answer
Follow this Question
Related Questions
Creating dynamically moving buttons/GUI objects. 1 Answer
[SOLVED] GUI Button Rotate animation Unity 5 1 Answer
Does multiple spamming a button break it? 2 Answers
OnTriggerEnter and UI button 2 Answers