- Home /
An on death pop up GUI
I am using this script on a character to have an enemy apply damage to the character.
var maximumhitPoints = 100;
var hitPoints= 100;
function ApplyDamage (damage:float) { if (hitPoints < 0) return;
//apply damage
hitPoints --;
if (hitPoints <= 0)
Die(); }
function Die() { if (hitPoints <=0)
//insert GUI here
}
but I'm having trouble understanding how to do GUI. All I want is for a window to pop up when you die that says you died and you can click an ok button. is this:
static function Window (id : int, clientRect : Rect, func : WindowFunction, content : GUIContent) : Rect
what I need to use? Thanks!
Answer by Yoerick · Nov 15, 2010 at 07:50 PM
If you want to write a GUI in code it should be done in the function OnGUI(), you can't write GUI code inside one of your own functions. In your case you could use a boolean to check if a player is dead or not, like so:
var isDead = false;
function OnGUI() { if (isDead) { GUI.Box(Rect(left, top, width, height), "You're dead"); if(GUI.Button(Rect(left, top, width, height), "Click here")) { isDead = false; } } }
function die() { if(hitpoints <= 0) isDead=true; }
what exactly are the values? are they just the size of the box?
and the error compiler keeps telling me i need a ; after the boolean
I changed the values so it's clear what they stand for ;) Sorry for the bool error, I usually code in c# and "bool" is a c# sharp type, in JavaScript it's just "var".
Answer by bhuszi · Nov 16, 2010 at 08:21 AM
function OnGUI() { print(""+boxselecting);
if(boxselecting){
var width : int = leftupbox.x - rightdownbox.x;
var height : int = (Screen.height - leftupbox.y) - (Screen.height - rightdownbox.y);
var rect : Rect = Rect(rightdownbox.x, Screen.height - rightdownbox.y, width,
height);
//GUI.Box(rect, "Its ALIVE");
GUI.Box(Rect(rightdownbox.x, Screen.height - rightdownbox.y, width, height), "Its ALIVE");
//GUI.DrawTexture(rect, selectionTexture, ScaleMode.StretchToFill, true, 10.0f);
print("GUI");
}
} this is my code for drwng a box and unity still gives me the GUI can only be called in OnGUI error whats wrong with my code?
Your answer
Follow this Question
Related Questions
GUI not counting Player Deaths 3 Answers
Javascript GUI and deathscript weird compiler errors 1 Answer
Help with calling and naming functions! 2 Answers
How will i get animated gif images in scene? 6 Answers
GUI.TextField problem, won't update var. 0 Answers