// Update is called once per frame void Update() { //RaycastHit hit; Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition); var hits = Physics.RaycastAll(ray, raycastLength); uiText.enabled = false; foreach (var hit in hits) { Transform objectHit = hit.transform; IInteract interactable = hit.collider.gameObject.GetComponent <IInteract>(); if (interactable != null && interactable.CanInteract()) { uiText.enabled = true; if (Input.GetKeyDown(KeyCode.E)) { Debug.Log("can interact with object."); interactable.Use(); } break; } } if (hits.Length == 0) { uiText.enabled = false; } }