S
S
Sergey Panov2018-05-30 18:19:03
C++ / C#
Sergey Panov, 2018-05-30 18:19:03

Why is the OnClick event lost when creating a prefab?

Good evening. I create several buttons (UI elements) of the Button type in the code with the Instatiate function.
They are created using a prefab. I need to hang up OnClick events on these buttons so that the method created in the script is executed.
On the stage, I first throw this Button, press the plus sign, throw a panel with a script into the OnClick event, select the method that will be processed and ... create a prefab, and the event disappears from the prefab. Why ?
How to handle the OnClick event of a button that I created with Instatiate in code?
Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2018-05-30
@select8

Because the object reference is stored in Onclick.
Prefab - cannot contain references to scene objects ..
or the script / method / object must be inside the prefab. or in the code from the script where you instantiate objects - add an event.
https://docs.unity3d.com/ScriptReference/UI.Button...
here, by the way, there is even an example.
how to shove it into a listener) you can not only a method of this class, but also any other, just get a link to it first (in your case, it will almost certainly fit to make a link to the script on the panel in the button generator)
something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question