U
U
Unmaskedguy2020-07-16 23:41:16
C++ / C#
Unmaskedguy, 2020-07-16 23:41:16

How to set a delegate to a Unity button when creating it?

I make a kind of inventory object by destroying the object and creating a Prefab and setting up its parts.

public GameObject InventoryItem;
    public Sprite sprites;
    public void Use()
    {
       GameObject toy = Instantiate(InventoryItem,GameObject.Find("Inventory").transform);
        toy.GetComponent<Image>().sprite = sprites;
        toy.AddComponent(typeof(Axe));
        //От тута не происходит то что я себе представляю.
        toy.transform.Find("ButtonUse").GetComponent<Button>().onClick.AddListener(delegate() { toy.GetComponent<Axe>().UseOnPick(); });

        Destroy(gameObject);
    }


Here is the delegate itself:
public void UseOnPick()
    {
        Text textField = GameObject.Find("Text").GetComponent<Text>();
        textField.text = "Это обычный Топор";
        new WaitForSeconds(5);
        textField.text = "";
    }


However, the buttons present in the prefab refuse to accept the delegate.
5f10ba8864487562904906.png
5f10ba98b9c31558034018.png
What kung fury technique to use?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question