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");
				}
			}
		}
	}

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");
        }
}

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.