Hi, i have a problem with Raycast and GUI i made simple pickup script for items and i wanna show GUI text. I made GUI text and it gets enabled as soon as raycast hits target tagged pickupable but if i look away i want that text to dissapear but that GUI text wont dissapear somewhy. My question is how can i disable GUI if raycast doesnt hit pickupable item Code (CSharp): if (Physics.Raycast (transform.position, transform.forward, out hit)) { if (hit.distance < 2) { if (hit.collider.gameObject.tag == "Pickupable") { GUI.SetActive (true); if (Input.GetKeyDown ("e")) { hit.collider.gameObject.SetActive (false); CollectSound.Play (); } } else if (hit.collider.gameObject.tag != "Pickupable") { GUI.SetActive (false); } } } else { GUI.SetActive (false); }
You have an "if" clause for an object being less than two, but you probably need an else clause for it where you set the GUI to false.
I tested the code and it's mostly working for me. However, when the hit object is more than 2 away it doesn't change. I made a few small changes and came up with this. Code (csharp): public class Test8 : MonoBehaviour { [SerializeField] GameObject text; void Update() { RaycastHit hit; bool shouldShow = false; if (Physics.Raycast(transform.position, transform.forward, out hit)) { if (hit.distance < 2) { if (hit.collider.CompareTag("Pickupable")) { shouldShow = true; if (Input.GetKeyDown(KeyCode.E)) { hit.collider.gameObject.SetActive(false); CollectSound.Play(); // I don't have this, so commented it out. } } } } text.SetActive(shouldShow); } }