- Home /
On Click paramaters disappear from button prefab?
I'm trying to learn how to use the new 4.6 GUI. I made a canvas with a button in the inspector. I added a script to another GameObject with a method to do something when the button was press. I added the GameObject along with the script and method information to the buttons OnClick area. Everything worked fine. Ran the program, pressed the button, things happened, no problems.
Then I tried to take that canvas and make it a prefab. The problem is when I Instantiate the canvas prefab (with the button attached) the OnClick info is no longer there and the button now does nothing. I have scrapped it and started from scratch several times but for the life of me I cannot figure out what the problem is. I was hoping someone here could enlighten me?
Thanks
I too am having this problem. Did you submit a bug report?
Do you solve this? I had ran into this problem at the moment, hope you can tell what caused this
Phew, it looks like latest version of unity Release Candidat 1 is fine :)
This problem hasn't yet been solved in Unity 5.0.1 Did you figure out a way to do it?
I barely remember what caused this problem, but I think in my case one of the buttons was overlapped by some other invisible canvas with graphic raycatrer blocking mouse input. Check out that your button gets mouse input correctly ![alt text][1]
in this case mouse is over "9" button and is checked (all fine). If not - some other canvas is may block your mouse input.
Well, I'm not sure if this related to your case, but hope it'll help [1]: http://habrastorage.org/files/61e/d73/d8e/61ed73d8e8c841069fa0628f0cf5cb23.png
Answer by architrathi · Apr 09, 2015 at 10:17 AM
Finally figured out a way to do this. Hope someone finds this answer useful.
To be able to add a button click to an instantiated prefab.
Add the script directly to your prefab.
Drag the attached script from your prefab to the OnButton click event of the same prefab.
Assign the function that you wish to be called using the Onbutton click handler.
Now, Apply the changes to the prefab and play the scene. The instantiated prefabs should have your onClick info attached.
Great!! But, in my app I have several buttons, and all of them load the same scene when they are pressed. However, the things that show the new scene depens on button pressed. How ca I make the new scene knows which button has been pressed?
Thank you for posting this, @architrathi! I came across the same problem(/bug?) and this workaround did the trick!
@jjuam, I think you either need a different script for each button or you need to have the buttons in a list, and then you can use List.IndexOf(thisButton) where thisButton is the GameObject of the clicked button.
Excellent answer. Unity $$anonymous$$m should have included this in their documentation.
I'm having a hard time understanding what you mean. Could you elaborate?
-PigChop
Thanks!!! I spent hours searching a solution, saved my day.
Answer by Dennis59 · Oct 08, 2016 at 07:32 AM
@PigChop, here is a way that you can instantiate a button from a prefab and attach any method that you wish. By setting this up as shown the parameter "value" will remain associated with the button that it is assigned to. So if you have "value = 1" when you create one button and "value = 2" when you create a second button then each button will retain that value. When you click on the button the delegate "myButtonDelegate" will receive the value that was assigned to it at instantiation. But they are fixed values and will always be what was assigned.
public void CreateMyButton()
{
int value = 1;
GameObject tButton = (GameObject)Instantiate(ButtonPrefab);
Button buttonCtrl = tButton.GetComponent<Button>();
buttonCtrl.onClick.AddListener(() => myButtonDelegate( value ));
}
void myButtonDelegate(int myValue)
{
// myValue will be equal to 1 here
}
Do these 'value' have any important purpose, or i can just ignore them?
It's a good solution and it works well, but the OnClick delegate in Unity Editor doesn't shows the "myButtonDelegate(int myValue)"... (Actually that's not to be a problem).
Answer by Tarrag · May 17, 2020 at 03:01 PM
five years on and @architrathi answer still works! thanks!
Answer by unimechanic · Oct 06, 2014 at 09:28 PM
You can ask in the Beta group, where our developers could help.
I would be happy too. Could you post a link? Thanks.
Answer by monkdream · Oct 11, 2016 at 10:23 AM
I was struggling with the exact same problem too. You can also make a prefab out of the GameObject your script is attached to and this allows the instantiated button to find the script. Prefabs everywhere ...
Please comment if this solution has caveats.
This is an old thread but this suggestion about “prefabs everywhere” is extremely wrong. You should only make prefabs where you need them. A prefab is essentially a blueprint for the object. If you use your button with an attached prefab in order to use the function, it will only use and change values from the prefab, and not from the objects in the scene during runtime. This can cause a lot of problems.
Your answer
Follow this Question
Related Questions
How do I access the button component of a button in the new gui system. 2 Answers
Unity 4.6 GUI weird toggle+button interaction? 2 Answers
GUI and buttons 3 Answers
Can't enable canvas 1 Answer
IPointerClickHandler error 2 Answers