How to trigger GUI objects to appear

I’d like to make a nice gui item list appear when I click on an object in my scene. I could not seem to find an example of this in the GUI docs.

I thought this would work (I always try the KISS method first).

function OnMouseDown () {
	GUI.Box (Rect (10,10,100,90), "Loader Menu");
}

It complied OK but when I click on the object I get this error…
NullReferenceException: Object reference not set to an instance of an object

Any help is appreciated

Kevin

GUI code needs to be inside an OnGUI() function.
You can get OnMouseUp to turn on the GUI when its needed - just pop a ShowGUI bool inside OnGUI().

If you’re making a Loader Menu (as your code seems to indicate) - I’d suggest using GUI.window. It’s more flexible in the long run.

You should read this before getting into the GUI.

Could you post some more detail about ShowGUI? I can’t find any reference to it in the GUI Guide or the Script reference.

I did look through the entire Gui Guide previous to my post but none of the examples deal with having it interact with other objects in the scene.

Any examples you could provide would be very helpful.

var showGUI = false;

function OnMouseDown() {
showGUI=true;
}

function OnGUI() {
if (showGUI) {
   GUI.Box (Rect (10,10,100,90), "Loader Menu"); 
}
}

I could swear that at one point in my own attempts I had exactly what you’ve posted - but of course my didn’t work and yours works perfectly:P

thanks

Kevin

It happens to us all. The new Unity GUI is like the force - it has a light and dark side… it’s just easier to stumble on the dark side :wink:

Since we’re on the subject, when I click on my object and the GUI window appears, is there a way to make the GUI window appear at the location of the object?

thanks

Kevin

Yes. You’ll just need to set the GUI Button’s rect so it’s “over” the selected model (or next to it, above it, whatever). You can use WorldToScreenPoint() to get the object’s position on screen and then build your Box rect relative to that:

var activeCamera : Camera;
var buttonRect : Rect;
var showGUI = false;

function OnMouseDown() {
  buttonPosn = activeCamera.WorldToScreenPoint(gameObject.transform.position);
  buttonRect = Rect(buttonPosn.x, buttonPosn.y, 100, 90);
  showGUI = true;
}

function OnGUI() {
  if (showGUI) {
    GUI.Box (buttonRect, "Loader Menu");
  }
}

You just need to drag-assign the current camera to the publicly exposed activeCamera variable. You need that to convert a world-position into an on-screen position so you can place the Box properly.

How would I go about doing the opposite. Placing a 3d object next to the GUI?

It would be a similar trick but instead of using WorldToScreenPoint you would use ScreenToWorldPoint to convert a screen position (the position of your GUI element) into a world position at which you place your GameObject of interest.

for this particular script would it be possible to use

input.GetKey function?
so it would look something like this *though it didn’t work for me im sure i am missing some stuff

var showGUI = false; 

function Update() { 
input.GetKey ("M"){
showGUI=true; 
} 

function OnGUI() { 
if (showGUI) { 
   GUI.Box (Rect (10,10,100,90), "Loader Menu"); 
} 
}

ignore me! i found what i needed :stuck_out_tongue:

Ignore Mode = On! :slight_smile:

(yes, you can, my button example was only one way to solve the problem)