- Home /
Need help with GUI and raycast...and gameobject I click on, I get the GUI to show up.
Like the title says, I need help-whenever I click on ANY gameobject, the GUI shows up. I want it so be so that whenever I click on a specific object, then and for ONLY that object, the GUI shows up. So if I click on any other gameobject or mesh, the GUI won't show up. Here's my script :)
using UnityEngine; using System.Collections;
public class Click : MonoBehaviour {
public Texture tex;
//public float GUItrialone = GUI.Button(350, 350, (10, 20 "Try!"))
bool displayGUI = false;
void OnGUI()
{
if (!tex)
Debug.LogError ("Really Dude? After all this, you forget a TEXTURE???");
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)
{
if(GUILayout.Button(tex))
displayGUI = false;
}
}
}
Thank you in advance!
Answer by ahmedbenlakhdhar · Dec 07, 2014 at 11:33 PM
Try to retrieve the game object from the RaycastHit, like this:
if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
{
if(outinfo.transform.gameObject.name == "your game object"){
displayGUI = true;
}
}
Is there a way to assign a gameobject as a variable, and then call that variable in the "if" logic you have there?
Because that did work, but I'm going to have like 14 objects, and I don't want to specifically mod a separate script for each one. So is there a way to assign, and then call a variable in the statement you have there?
Yes, but why doesn't this work well enough?
public GameObject $$anonymous$$YOBJECT = null; //Set this in the editor
if (Physics.Raycast(ray, out outinfo, $$anonymous$$athf.Infinity))
{
if(outinfo.transform.gameObject == $$anonymous$$YOBJECT){
displayGUI = true;
}
}
Well, I'm dealing with a ton of gameobjects, so for me to put this script on everything, and then change it for each object, would turn out to be quite the hassle, and it is BOUND for error, seeing as I am a bit clumsy at my editing :)
But thanks, to both of you. If there are any errors, I will post them here, possibly as an edit, or answer, or comment....