- Home /
UI button doesn't appear - c#
Hi
In my game when the player falls of the edge and touches a trigger a gui buttom should appear however it doesn't.
using UnityEngine;
using System.Collections;
public class GameOver : MonoBehaviour {
public GameOverScript GameOverVar;
public bool isClicked = false;
bool guiOn = false;
public float OnX = 15f;
public float OnY = 15f;
public float width = 100f;
public float height = 50f;
public string textInGUI = "Try again?";
void OnTriggerEnter2D (Collider2D over)
{
if(over.gameObject.tag == "Player")
{
GameOverVar.isGameOver = true;
if (GameOverVar.isGameOver == true)
{
//Application.LoadLevel(1);
guiOn = !guiOn;
}
}
}
void OnGUI()
{
if(guiOn)
{
GUI.Button(new Rect(OnX,OnY,width,height), textInGUI);
Debug.Log("It worked");
guiOn = false;
}
}
}
Also the Debug.Log("It worked"); does output in the console
Answer by Rad-Coders · Jul 12, 2015 at 01:54 PM
The right code, and have been tested so it works.
if(guiOn)
{
bool button = GUI.Button(new Rect(OnX,OnY,width,height),textInGUI);
if(button)
{
Debug.Log("It worked");
guiOn = false;
}
}
Answer by barbe63 · Jul 11, 2015 at 04:34 PM
By setting guiOn to false immediatly after displaying it you only display it for one frame. Just remove the guiOn = false or maybe put it like this:
void OnGUI()
{
if(guiOn)
{
if(GUI.Button(new Rect(OnX,OnY,width,height), textInGUI))
{
Debug.Log("It worked");
guiOn = false;
}
}
}
Try this if(guiOn) { bool button = GUI.Button(new Rect(OnX,OnY,width,height), textInGUI); if(button) { Debug.Log("It worked"); guiOn = false; } }
Answer by Rad-Coders · Jul 12, 2015 at 01:53 PM
The problem here is that once you make guiOn false it hides the button because the button will only show as long as guiOn is true. Try to make guiOn false once the player has clicked on the button you cando this all at once like this.
if(guiOn)
{
if(GUI.Button(new Rect(OnX,OnY,width,height), textInGUI))
{
Debug.Log("It worked");
guiOn = false;
}
}
This should work. And it creates the button so just replace that code with this one.
Your answer
Follow this Question
Related Questions
How to fire gui button on mouse right release 2 Answers
C# Boolean Doesn't Change Value 1 Answer
Trigger Sound more than Once 0 Answers
How to make a or array of GUI.Button's? 1 Answer
GUI.Button is acting funky. 2 Answers