- Home /
GUI trouble
I have a GUI that pops up when I die that has a play again button. I have it so that if I have more than 0 lives, then it lets me play again. but if I have 0 lives and I press it, it should pop up a box saying "You are out of Lives" or something like that. But if I press it with 0 lives, it does nothing. Here is the snippet of code:
if(GUI.Button(new Rect(GameoverRect.x + GameoverRect.width - (Screen.width * .25f), GameoverRect.y + GameoverRect.height - (Screen.height * .25f), Screen.width * .25f, Screen.height * .2f), "", GUI.skin.GetStyle("PlayAgain")))
{
if(Lives > 0)
{
//AudioSource.PlayClipAtPoint(ButtonPressed, transform.position);
Application.LoadLevel(GameplayLevel);
}
else
{
GUI.Box(new Rect(Screen.width/2 - (Screen.width * .3f/2), Screen.height/2 - (Screen.height * .3f/2), Screen.width * .3f, Screen.height * .3f), "You need more Lives!", GUI.skin.GetStyle("OutOfLives"));
}
I have tried replacing "else" with "if(Lives
Comment
Best Answer
Answer by zharik86 · Aug 21, 2014 at 07:00 PM
Your code work correct, but you see box only one frame. See example below:
private float timingBox = 0.0f; //variable for time of see our box
void Update() {
if (timingBox > 0) {
timingBox = timingBox - Time.delataTime;
}
}
void OnGUI() {
//First you change GUI.skin and etc operation, than
if(GUI.Button(new Rect(GameoverRect.x + GameoverRect.width - (Screen.width * .25f), GameoverRect.y + GameoverRect.height - (Screen.height * .25f), Screen.width * .25f, Screen.height * .2f), "", GUI.skin.GetStyle("PlayAgain"))) {
if(Lives > 0) {
//AudioSource.PlayClipAtPoint(ButtonPressed, transform.position);
Application.LoadLevel(GameplayLevel);
} else {
timingBox = 2.0f;
}
}
if (timingBox > 0) { //show box
GUI.Box(new Rect(Screen.width/2 - (Screen.width * .3f/2), Screen.height/2 - (Screen.height * .3f/2), Screen.width * .3f, Screen.height * .3f), "You need more Lives!", GUI.skin.GetStyle("OutOfLives"));
}
}
I hope that it will help you.