Issue
i want to change if (Input.GetKeyDown (KeyCode.E)) with UIButton and I can't, help me...
private void OnTriggerStay(Collider col)
{
if (col.gameObject.CompareTag("Item"))
{
if (Input.GetKeyDown(KeyCode.E))
{
playerInventory.item.Add(GetComponent<Item>());
Destroy(col.transform.gameObject);
}
}
}
Solution
First of all I would assume for now that you actually would want to use col.GetComponent<Item>()
instead of trying to get the component from the very same object this script is attached to.
And then instead of using OnTriggerStay
you could instead do something like
private readonly HashSet<Item> currentTriggeredItems = new HashSet<Item>();
private void OnTriggerEnter(Collider col)
{
if (col.CompareTag("Item") && col.TryGetComponent<Item>(out var item))
{
if(!currentTriggeredItems.Contains(item)) currentTriggeredItems.Add(item);
}
}
private void OnTriggerExit(Collider col)
{
if (col.CompareTag("Item") && col.TryGetComponent<Item>(out var item))
{
if(currentTriggeredItems.Contains(item)) currentTriggeredItems.Remove(item);
}
}
// This is the method you reference in the UI.Button.onClick
public void OnUiButtonClicked()
{
foreach(var item in currentTriggeredItems)
{
playerInventory.item.Add(item);
Destroy(item.gameObject)
}
currentTriggeredItems.Clear();
}
Answered By - derHugo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.