- Home /
Questions about GUI?
So I am trying to create a GUI for when the player dies (Losing health, falling off the map, etc.). Here is my code:
function OnDeath() { // Lose Health on Death health--;
// Reload entire level if (health != 0) { Spawn(); } else if (health == 0) { Destroy(gameObject); print("You have died."); } // Lose Score on Death new MessageScorePoints(-dieLosePoints);
}
So you can see that if "health != 0", the player spawns. But if "health == 0", I would like a Box to show up that has 3 options: "Start from Last Checkpoint", "Exit to Menu", and "Exit Game". How would I do this? I tried using OnGUI but that seems it is always on the screen and can't just call/uncall it as I wish?
PS - Yes, I am quite new to Unity's API. Been working with the editor for awhile, but just recently got into the actual scripting of it. Only JavaScript, though.
Answer by Uriel_96 · Feb 05, 2011 at 03:00 AM
for do that kind of things you must use labels, buttons,textfield, and many others, to see more about it go to this page: http://unity3d.com/support/documentation/ScriptReference/GUI.html here you can find several ways to do what you want.
function OnGUI(){
if(health == 0){
//put your labels, buttons, textfield, etc..
}
}
this will make that it only appears when you variable health is 0 if is not 0 will disappear.
Sorry, I must not have made myself that clear. I tried using "function OnGUI()", but it seems that there is no way to actually make that appear/disappear when I want? For example, only for the box/buttons to appear when "health == 0".
have you try to put something like: function OnGUI(){if(health == 0){/put your GUIs here/}}
that may work because I actually do it sometimes and it works.
Answer by BlackBoy0 · Feb 05, 2011 at 08:30 AM
Oh I see what I did wrong. In the OnDeath function, when the "health == 0", I called "Destroy(gameObject);", which I guess destroys the entire gameObject, thus having no "OnGUI" to run. So I simply commented that out and the GUI started to work. Thank you Uriel :)
Your answer
Follow this Question
Related Questions
GUI DrawTexture is sliding across the screen? 0 Answers
Health system 2d 2 Answers
[2D][Beginner]How do I heal my character when pressing a key while next to a health pack? 1 Answer
Drag and release 2 Answers
Rigidbody Collider Hero health does not drain when touched by an enemy rigidbody collider. 2 Answers