Ваш подход возможен путем изменения мелочи. В настоящее время вы пытаетесь добавить слушателя прямо в ваш префаб. Вместо этого вы должны добавить слушателя к кнопке, которую вы создали с помощью своего префаба.
Пример, который должен работать:
public GameObject itemButtonPrefab; // drag your Prefab to this field in the inspector
public Button itemButton; // the "real" instance of your prefab in the scene
void AddListeners()
{
itemButton.onClick.AddListener(() => GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>().GainHealth(50));
}
void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
for (int i = 0; i < inventory.slots.Length; i++)
{
if (inventory.isFull[i] == false)
{
inventory.isFull[i] = true;
itemButton = Instantiate(itemButtonPrefab, inventory.slots[i].transform, false).GetComponent<Button>();
AddListeners ();
Destroy(gameObject);
break;
}
}
}
}
PS: этот пример также должен работать немного более производительно из-за того, что вам не нужно вызывать метод GetComponent:
public Button itemButtonPrefab; // drag your Prefab to this field in the inspector
public Button itemButton; // the "real" instance of your prefab in the scene
void AddListeners()
{
itemButton.onClick.AddListener(() => GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>().GainHealth(50));
}
void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
for (int i = 0; i < inventory.slots.Length; i++)
{
if (inventory.isFull[i] == false)
{
inventory.isFull[i] = true;
itemButton = Instantiate(itemButtonPrefab, inventory.slots[i].transform, false);
AddListeners ();
Destroy(gameObject);
break;
}
}
}
}