trigger + key press toggles GUI Texture

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.

3 Answers

3

OK I got it to work. Here is the code I am using, in case it helps someone else trying to do something similar.

var showPhoto : boolean;
var windowRect : Rect;
var AddPhoto : Texture2D;

function OnGUI(){
   if(showPhoto){
      GUI.Box( windowRect, AddPhoto );
      windowRect = Rect( 0,0, 800,600); 
   }
}


function OnTriggerStay(){
   if(Input.GetKeyDown( KeyCode.L )){
   //showPhoto = true;
   showPhoto = !showPhoto;
}
   
}

function OnTriggerExit(){
   
   showPhoto = false;
   
}

Basically this javascript allows you to use the “L” key to toggle a GUI image on and off, which is 800 x 600 in size, starting at the top left of your screen. It will only toggle on/off if you are standing in a trigger area ( like a cube set to trigger)
You can turn the GUI off by hitting “L” again, or just by walking out of the trigger area it will also turn the GUI off.
If someone out there has a cleaner way to write this script, feel free to add to this thread. Thanks.

I have been working on a problem for a month and your solution also solved my issue also! Thank you for posting SO much!

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); } }

is it possible to use image from web renderer.material.mainTexture = www.texture; in this? thanks