- Home /
How to change OnTriggerEnter to Ui Button click
Hello, Im trying to make shop system with inventory (using Invector). Invvector has a code wich adds items into your inventory, but that code is based on OnTriggerEnter(). I want to make that code to work, when i press Buy button in my shop. I tried to do OnMouseDown(), but that didnt worked. Please, can you help me?
using UnityEngine; using System.Collections; using Invector.ItemManager;
public class vAddItemByID : MonoBehaviour { public int id, amount; public bool autoEquip; public bool destroyAfter;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
var itemManager = other.gameObject.GetComponent<vItemManager>();
if (itemManager)
{
var reference = new ItemReference(id);
reference.amount = amount;
reference.autoEquip = autoEquip;
itemManager.AddItem(reference);
}
if (destroyAfter) Destroy(gameObject);
}
}
}
Answer by hexagonius · Mar 11, 2018 at 10:02 AM
Create a public function in the same script that calls OnTriggerEnter. In your UI Button reference the script in the OnClick event and choose the new public function
How to write this script? Because this script is not working.
public void PressButton()
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
var item$$anonymous$$anager = other.gameObject.GetComponent<vItem$$anonymous$$anager>();
if (item$$anonymous$$anager)
{
var reference = new ItemReference(id);
reference.amount = amount;
reference.autoEquip = autoEquip;
item$$anonymous$$anager.AddItem(reference);
}
if (destroyAfter) Destroy(gameObject);
}
}
}
}