2

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.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    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 Feb 28 '16 at 10:30
  • 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. –  Feb 28 '16 at 10:42
  • 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 Feb 28 '16 at 13:37

3 Answers3

4

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).

Thomas Hilbert
  • 3,559
  • 2
  • 13
  • 33
  • 1
    Hi Thom., of course your answer is correct. I'm not sure why you mention /Net events being easier to use than `UnityEngine.EventSystems` ... I mean, `UnityEngine.EventSystems` is completely built in: you just drag over the Component you want to signal. Could you give me an example of how to use .Net events in the way you're thinking? – Fattie Feb 28 '16 at 13:18
  • 1
    You're right. That event can of course still be a standard Unity event. I actually wanted to make another point. If you spawn prefabs at runtime, you usually don't want to have hard coded assumptions about the inner structure of that prefab. You'd much rather reference the prefab by a basic behaviour type like "BaseDialog" or whatever. You know it will have some OK button somewhere, but you don't want to know where. The easiest way would be to have that BaseDialog publish an OKClicked event and hide the internal wiring up from the user. – Thomas Hilbert Feb 28 '16 at 14:23
  • Thanks for the clarification, Thomas. Yes, the object is a prefab, does not get created at runtime; it exist in my resource folder so I can crete and destroy it every time I need. I thought that this approach is the most straight forward, for a game that show many panels, where users either get info from them, or modify parameters. Instead than show and hide panels all the time; I just load a prefab; show the data, save the data where needed and when they close the pane, it just get destroyed. I did try adding via code the event, to the button, and it works fine. just 3 more lines of code. –  Feb 28 '16 at 23:02
  • hi @newbiez. in your last comment, you suggest: **instead of using a "prefab" as such, just have a "model" waiting in your scene, and use that**. In fact you will be pleased to hear ***THAT IS EXACTLY CORRECT -- THAT IS OFTEN THE BEST THING TO DO***. Indeed some teams never use prefabs, and only use the "model sitting in the scene" idea. You're 100% correct. ***DEFINITELY DO THAT IF YOU WISH***. Master both techniques. – Fattie Feb 29 '16 at 17:08
1

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.

radiodario
  • 156
  • 7
0

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.

pedjaradenkovic
  • 241
  • 1
  • 9