- Home /
How to make a GUI button
I am trying to do a "next level" button that will load the next level. But after that i need to wait 3 seconds before the button appears. I am using a StartCorountine function for doing that. i know that i need to put the code for button in the void OnGUI(). Here's my code. The button does not appear because its not on the void ONGUI() function. but if i put it there the button will be created no matter what, which is not what i want. I need a button to be spawned after 3 seconds. Thank you very much.
void OnGUI(){
if (GameObject.FindObjectOfType<DeathTrigger> ().hasLost == false && GameObject.FindObjectOfType<ScoreManager>().score==5) {
GUI.color = Color.black;
GUI.Label (new Rect (Screen.width / 2 - 50, Screen.height / 3, 200, 100), "Nailed it!! Stay still for 3 seconds for next Level");
StartCoroutine("waitforme");
}
}
IEnumerator waitforme(){
yield return new WaitForSeconds(3);
if (!GameObject.FindObjectOfType<DeathTrigger> ().hasLost) {
print ("Just got here bro");
if( GUI.Button( new Rect(Screen.width/2-50, Screen.height/2-25 + 55, 100, 50), "Next Level") )
Application.LoadLevel ("scene 1");
}
}
Answer by DoTA_KAMIKADzE · May 22, 2015 at 02:08 PM
The easiest way with smallest amount of changes in your code:
private bool buttON = false;
void OnGUI()
{
if (GameObject.FindObjectOfType<DeathTrigger>().hasLost == false && GameObject.FindObjectOfType<ScoreManager>().score == 5)
{
GUI.color = Color.black;
GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 3, 200, 100), "Nailed it!! Stay still for 3 seconds for next Level");
StartCoroutine(waitforme());
}
if (buttON) if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 25 + 55, 100, 50), "Next Level")) Application.LoadLevel("scene 1");
}
IEnumerator waitforme()
{
yield return new WaitForSeconds(3);
if (!GameObject.FindObjectOfType<DeathTrigger>().hasLost)
{
print("Just got here bro");
buttON = true;
}
}
It is also possible to count time in other ways using Time variables if for some reason you don't like coroutine.
Your answer
Follow this Question
Related Questions
gui text button dont work 1 Answer
How to make a or array of GUI.Button's? 1 Answer
Create 3D GUI Button? 2 Answers
Interactable buttons are disabled? 3 Answers
Embed GUI Skin in GUI Button 2 Answers