- Home /
GUI Box appear when you are close to something?
Hi, I have a door that I have connected to another scene, and I want to have it so that once I get close to the door, a GUI Box will popup to tell the player where the door goes. What I was thinking was that in the Update Function, once the player gets close to the door, this will set a boolean true, and then in the OnGUI function, if that boolean is true, then the GUI box would appear, but this is not happening. When I get close to the door, nothing happens. Here is my script:
#pragma strict
var player : GameObject;
var sheddoor : GameObject;
var playersheddoordist = Vector3.Distance(player.transform.position, sheddoor.transform.position);
var guibox = Rect(190, 175, 160, 160);
var guiboxboolean : boolean = false;
function Start () {
}
function Update ()
{
if (playersheddoordist > 3)
{
guiboxboolean = true;
}
else
{
guiboxboolean = false;
}
function OnGUI ()
{
if (yesnoone)
{
GUI.Box (guibox, "Enter Shed?");
}
}
Any ideas of how I could fix this? Thanks in advance.
Does this script compile? It looks like variable yesnoone is not declared anywhere we can see.
Also, guiboxboolean goes to true if player is greater than 3 units from the door. Should this be less than?
Lastly, I would think yesnoone should actually be guiboxboolean.
Alright, that was the problem, I did what alucardj said and it worked, Also, yea, it should have been less than rather than more than, olgo, and I had forgotten to copy the declaration of variable yesnoone. Thanks!
Answer by AlucardJay · Apr 04, 2013 at 06:08 PM
You cannot do a calculation outside of a function :
var playersheddoordist = Vector3.Distance(player.transform.position, sheddoor.transform.position);
this should be :
var playersheddoordist : float;
function Update ()
{
playersheddoordist = Vector3.Distance(player.transform.position, sheddoor.transform.position);
if (playersheddoordist > 3) // etc ....
or simply :
function Update ()
{
var playersheddoordist : float = Vector3.Distance(player.transform.position, sheddoor.transform.position);
if (playersheddoordist > 3) // etc ....
Your answer
Follow this Question
Related Questions
Hiding OnGUI? 1 Answer
Drawing a GUI box by specifying the two corners 1 Answer
How completly remove GUI.Repaint() calls ? 1 Answer
full screen wide GUI.Box 3 Answers