I am trying to make a GUI texture display once two conditions are met.
In my game I want to be able to bring up a picture of a note when standing in a certain location and I hit the “L” key to look at the note. Then by hitting the “L” key again I want it to toggle the GUI off, to hide the note from view.
I set up my world as follows. I am using the standard assets FPS controller. I placed a box on the floor and set it as a trigger. I placed the following script on the box. IN the inspector I placed a picture of a note in the AddPhoto place holder:
var showPhoto : boolean;
var windowRect : Rect;
var AddPhoto : Texture2D;
function OnGUI(){
if(showPhoto){
GUI.Box( windowRect, AddPhoto );
windowRect = Rect( 0,0, 800,600);
}
}
function OnTriggerEnter(){
if(Input.GetKeyDown( KeyCode.L )){
showPhoto = true;
}
}
I must not be scripting it properly because if I select Show photo in the inspector. It shows the GUI all the time. If I don’t check it off in inspector it doesn’t show up at all.
Please help me, …I’m really frustrated.
Also I found this code on this forum:
var visible;
var windowRect;
var AddPhoto : Texture2D;
function Start()
{
visible = false;
windowRect = Rect( 0,0, 800,600);
}
function OnTriggerEnter(other : Collider) {
OnGUI();
}
function OnGUI()
{
if( visible )
{
GUI.Box( windowRect, AddPhoto );
}
}
function Update()
{
if( Input.GetKeyDown( KeyCode.L ) )
{
visible = !visible;
}
}
This makes the GUI texture toggle on and off, but I can’t get it to work with a trigger event.
I found a more reliable solution: public Vector2 MousePosition { get { var mousePos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y); return RuntimePanelUtils.ScreenToPanel(_root.panel, mousePos); } }
– Oneiros90