I’ve Implemented a UI to pickup Items instead of clicking the mouse button. But When I have like 3 pickups in an area and I picked up everything, the UI doesn’t hide again. What am I missing?
So with the ItemCollector on the user like this.
public class ItemCollector : MonoBehaviour, IAction
{
Pickup target;
void Update()
{
if (target == null) return;
if (!GetIsInRange(target.transform))
{
GetComponent<Mover>().MoveTo(target.transform.position, 1);
}
else
{
target.PickupItem();
target = null;
}
}
public void CollectItem(Pickup pickup)
{
GetComponent<ActionScheduler>().StartAction(this);
target = pickup;
}
private bool GetIsInRange(Transform targetTransform)
{
return Vector3.Distance(transform.position, targetTransform.position) < 3;
}
public void Cancel()
{
target = null;
GetComponent<Mover>().Cancel();
}
}
Then the ClickablePickup script
public class ClickablePickup : Pickup, IRaycastable
{
Pickup pickup;
bool isInRange = false;
PickupUI pickupUI;
private void Awake()
{
pickup = GetComponent<Pickup>();
pickupUI = FindObjectOfType<PickupUI>();
}
public CursorType GetCursorType()
{
return CursorType.Pickup;
}
public bool HandleRayCast(PlayerController callingController)
{
if (Input.GetKeyDown(KeyCode.E))
{
callingController.GetComponent<ItemCollector>().CollectItem(pickup);
}
return true;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
pickupUI.ShowUI();
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
pickupUI.HideUI();
}
}
}
Then I have a simple UI to show or hide a UI telling the user to click “[E] Pickup” like so:
public class PickupUI : MonoBehaviour
{
[SerializeField] private GameObject panel = null;
[SerializeField] private TextMeshProUGUI text = null;
private void Start()
{
HideUI();
}
public void ShowUI()
{
panel.SetActive(true);
text.text = "[E] Pickup ";
}
public void HideUI()
{
panel.SetActive(false);
}
}