How to make a GUI Appear when a GameObject is clicked

What would the code be for making an plane clickable and on tha click it will open a GUI that i have created?!

anybody????

2 Answers

2

void OnMouseDown ()
{
if (gameObject.tag == “Insert Tag”) {
_OpenGUI = true;
}
}

void OnGUI ()
	{
		if (_OpenGUI){
			//GUI Code;
            }
    }

might sound stupis, but what is the tag?

Tag let you the choice on which GameObject you want to allow to open. You can remove it if you want.

[GameObjects][1] - read this to find out about them. if you use the code i pasted you will set the tag exactly to what is in the quotations. [1]: http://docs.unity3d.com/Documentation/Components/class-GameObject.html

In this case the tag is not necessary. This script makes the specific object it is attached to execute code if the mouse is clicked on this specific object. If you didn't want this specific object to be clickable, you would not attach the script in the first place. GameObject.tag is helpful when you find arbitrary objects in the scene typically using Raycasting and want to figure out if some action should be taken.

Ahh... My mistake, i thought OnMouseDown worked like raycast where it would initiate if it collided with "ANYTHING".

Hi,

this is one way to your question.

Your GameObject need a collider with Trigger set as true.

bool        displayGUI = false;
void	OnGUI()
{
	if (Input.GetMouseButtonUp(0))
	{
		Ray	    	ray = Camera.main.ViewportPointToRay(Camera.main.ScreenToViewportPoint(Input.mousePosition));
		RaycastHit	outinfo;

		if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
		{
			//TOUCH! Show GUI
			displayGUI = true;
		}
	}
    if (displayGUI == true)
    {
            //GUILayout.YourStuff();
    }
}

Thanks for the help! But this code has a few errors in it and I wasn't able to use it. How could I fix this?

The behaviour of this script is to set a boolean to true when you click on the collider. It won't disappear. What kind of error do you have? Compile? Behaviour? I've just put this code in a MonoBehaviour script and it works. Remember, the Game Object needs a Collider. Trigger doesn't matter.

It was a compiler error. Mainly near the top of the script.

Maybe, can you write it here? I can't help you with nothing. XD Check if you copied the code well, or if you already have a OnGUI() function or a displayGUI variable.