What would the code be for making an plane clickable and on tha click it will open a GUI that i have created?!
2 Answers
2void OnMouseDown ()
{
if (gameObject.tag == “Insert Tag”) {
_OpenGUI = true;
}
}
void OnGUI ()
{
if (_OpenGUI){
//GUI Code;
}
}
Tag let you the choice on which GameObject you want to allow to open. You can remove it if you want.
– Mikilo[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
– Sooper1337In 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.
– robertbuAhh... My mistake, i thought OnMouseDown worked like raycast where it would initiate if it collided with "ANYTHING".
– Sooper1337Hi,
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?
– buzzhead1994The 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.
– MikiloMaybe, 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.
– Mikilo
anybody????
– buzzhead1994