2
votes

I have a simple prefab, made by a prefab button and a panel. The button is placed inside the panel so it is a child of the panel. I did save the prefab and all is good.

Then, I did add a click event on the button; so I did add the gameobject where the script that I want to use is placed, and select the correct method to use. I did update the prefab and deleted it from the scene.

Now, when I instantiate this prefab in the scene with Resources.Load("myUIprefab"); it show correctly the panel, the button and the colors/settings.

But there is nothing connected to the click button itself; which result in the button doing nothing.

Isnt' a prefab, a way to save an object to be used at later time? In that case it should retain the click settings. Is this a bug or an unfortunate implementation of the new Unity UI system? Makes little sense to make a prefab out of a button or another GUI element, if then the click() or other delegate are not filled.

3
I quess that the onclick method you have choosen is from hierarchy object not the prefab. So when you create prefab it looses its connection with hierarchy. To fix this you need to assign onclick handler with code. - Jerry Switalski
I see, so if the button,which is a prefab (mainly because of the graphic style and color), get assigned an onclick(); I loose it even if I save this modified prefab in another prefab? I guess that I need to add via code then the delegate to my method, via addlistener of the button. Thanks. - user393267
Jerry dude - what possible purpose would it serve to add a handler call inside the prefab. Say you did that. Then, when you put the prefab in a scene: you have to drag to connect to that handler. Why the heck would you do that? Just drag to connect to the button, no difference! - Fattie

3 Answers

4
votes

GameObjects that are stored in a prefab can only hold references to other objects inside that same prefab, or to other prefabs. This is true for object references, events, whatever.

If you want the event to persist, you must have an event handler in the same prefab and have that called by your button. This can be a simple handler method that raises a standard .Net event when called (.Net events may be easier to use than Unity events).

1
votes

Sorry I'm late to the party. I managed to keep the event data setup correctly by just keeping an instance of the prefab deactivated on the scene that had the correct settings, and then pointing the behaviour that spawns them to this object rather than the prefab.

0
votes

In a case, you want to call a method defined in the script attached to an unrelated game object (for example GameManager) you have to make this other game object as prefab and assign an onclick method to this prefab.