- Home /
 
How would I bring up a GUI after a raycast has hit a gameObject with a specific tag?
I've got the raycast to hit the gameObject and print a message if the tag == "Vendor". However I tried to create a GUI.Box and it didn't work, I'm doing this under OnGUI() also.
I'm a bit hesitant to post my code as I have only just started, so don't be too harsh if my code isn't up to scratch, haha.
Anyway, this is what I have so far:
 void OnGUI ()
     {
         if(Input.GetKeyUp (KeyCode.E))
         {
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             RaycastHit hit;
             if (Physics.Raycast(transform.position, fwd, out hit, 2))
             {
                 if(hit.collider.gameObject.tag == "Vendor")
                 {
                     GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 200), "Vendor's Inventory");
                 }
             }
         }
     }
 
              Answer by dsada · Sep 19, 2014 at 02:47 PM
This is not the right direction. The raycast part is okay, but you are using it badly. The first thing you need to know is that Input functions only work well in the Update() function. The second thing that you need to know is that if you would like to show some GUI related things, you need that call everytime OnGUI is called. Imagine those things like local variables. So to satisfy both restrictions you need to transform your script like this:
 private bool hitSuccess = false;
 
 void Update()
 {
     if(Input.GetKeyUp (KeyCode.E))
         {
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             RaycastHit hit;
             if (Physics.Raycast(transform.position, fwd, out hit, 2))
             {
                 if(hit.collider.gameObject.tag == "Vendor")
                 {
                     hitSuccess = true;
                 }
             }
         }
 }
 
 void OnGUI ()
 {
         if(hitSuccess)
         {
            GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 200), "Vendor's Inventory");
         }
 }
 
              Thank you very much man! This worked like a charm, and I also edited it to allow a button to close the window using the bool as well. Thank you again!
NeinZisIsFrom$$anonymous$$einSpringCollectionJa, would this be the appropriate method to use if I wanted to look at an object in the distance, then bring it up on a GUI to list the text written on it?
Answer by dmg0600 · Sep 19, 2014 at 03:02 PM
It's not that it doesn't work, it is that the box is only displayed for one frame when the GetKeyUp triggers.
You should use a local bool variable to check if the box should be displayed. That way it will be displayed when the variable is true and not only for one frame.
It is better, as dsada pointed out to use the Input and Raycast part of the script in Update. The thing that is missing in that script is to toggle the box off.
 void Update () {
     if (Input.GetKeyUp(KeyCode.E))
     {
         showBox = false;
 
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         RaycastHit hit;
         if (Physics.Raycast(transform.position, fwd, out hit, 2))
         {
             Debug.Log("Choco con " + hit.collider.name);
             if (hit.collider.gameObject.tag == "Vendor")
             {
                 showBox = true;
             }
         }
     }
 }
 
 void OnGUI()
 {
     if (showBox)
         GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 200), "Vendor's Inventory");
 }
 
               Setting the variable to false at the beggining of the if, the box will toggle off whenever you press E and the Raycast doesn't hit the vendor.
Your answer
 
             Follow this Question
Related Questions
OnMouseUp() not working like meant 1 Answer
Raycast shooting in the middle of the screen 0 Answers
GUI label on raycast? 0 Answers
Raycast behavies as if its snapping to grid on standalone build 1 Answer
Trouble targeting GUI with raycast 0 Answers