0
votes

I'm having a bit of trouble with some Unityscript.

What I want to do is have a GUI message appear when certain objects are touched (then vanish after a time). I think I have it mostly worked out, but the message trips automatically.

My attempted solution is to have a conditional part of the GUI message that only allows it to appear when a boolean is true. Then in a different script that is already tripped when the object is touched, the boolean is set to true, so the script can run, and reset the boolean to false.

However I'm getting a "You can only call GUI functions from inside OnGUI. I'm not sure what that means.

Message code:

youdied.js
static var deathMessageShow : boolean = false;

function OnGUI() {
if(deathMessageShow == true){
    if(Time.time >= 5 )
        GUI.Box(Rect(200,300,275,150),"You Died");
    }
deathMessageShow = false;
}

Other code (truncated):

dead.js
function OnTriggerEnter()
{
//code that resets environment
youdied.deathMessageShow = true;
}

Any suggestions on what is going on, or a better solution would be greatly appreciated.

1
You really should call that variable isDeathMessageShown or so. Everyone would expect a deathMessage to be a string :-) - Bergi
You should really consider working on the structure of this code (you at least store an instance of YouDied in a public variable instead of using a static variable). Also maybe there's some context missing, but Time.time > 5 probably won't do what you want it to. It means that 5 seconds after the game starts you'll no longer show messages (any messages, Time.time is the time since the game started [or you pressed play in the editor, which might make it seem like it's working]) - Selali Adobor
Hmm, okay, how should I have it run for 5 seconds from the moment it is triggered? - MechaScoots

1 Answers

0
votes

The following code will show GUI message for 5 seconds then hides it:

youdied.js
static var deathMessageShow : boolean = false;

function OnGUI()
{
    if(deathMessageShow)
    {
        GUI.Box(Rect(200,300,275,150),"You Died");

        // call the function HideDeathMessage() after five seconds
        Invoke("HideDeathMessage", 5.0f);
    }
}

function HideDeathMessage()
{
    deathMessageShow = false;
}

Other code:

dead.js
function OnTriggerEnter()
{
    //code that resets environment
    youdied.deathMessageShow = true;
}