V
V
valek972020-09-07 03:40:33
Unity
valek97, 2020-09-07 03:40:33

How to add event handling to a button clone through a script?

There is a base scene. There is a menu. When you click on the button, the menu is deleted, when you click on the field, it is created. Faced the problem that the clone of the prefab does not copy the value (onClick) from the parent. Can't add value to OnClick.
Tried the following:

GameObject shopObj = Instantiate(ShopPref);
               
                shopObj.transform.SetParent(GameObject.Find("Canvas").transform, false);
                //Создает успешно канвас, но у кнопки отсутствуют события которые есть у родителя
                GameObject CloseButton = GameObject.Find("CloseButton");
                if(CloseButton == true)
                {
                    Debug.Log(CloseButton.name);
                }
                CloseButton.GetComponent<Button>().onClick.AddListener(GetComponent<ShopScr>().CloseShop);
                 //Button CloseButton = GetComponent<Button>();
                //CloseButton.onClick.AddListener((GetComponent<ShopScr>().CloseShop));

What can you think of or are there other options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Fobius, 2020-09-07
@phobious

Isn't it easier to move / make the menu invisible, and vice versa? I think it's much easier, smooth disappearance, or just a shift off the screen (canvas / camera)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question