Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by michellejean · Sep 21, 2014 at 07:01 AM · guiprefabbuttoncanvas4.6

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

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WrGraban · Sep 30, 2014 at 03:54 AM 0
Share

I too am having this problem. Did you submit a bug report?

avatar image Deadcow_ · Nov 09, 2014 at 01:25 PM 0
Share

Do you solve this? I had ran into this problem at the moment, hope you can tell what caused this

avatar image Deadcow_ · Nov 09, 2014 at 03:57 PM 0
Share

Phew, it looks like latest version of unity Release Candidat 1 is fine :)

avatar image architrathi · Apr 08, 2015 at 03:36 PM 0
Share

This problem hasn't yet been solved in Unity 5.0.1 Did you figure out a way to do it?

avatar image Deadcow_ · Apr 08, 2015 at 04:14 PM 0
Share

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

Show more comments

5 Replies

· Add your reply
  • Sort: 
avatar image
34

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.

  1. Add the script directly to your prefab.

  2. Drag the attached script from your prefab to the OnButton click event of the same prefab.

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jjuam · Sep 06, 2015 at 09:32 AM 0
Share

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?

avatar image MountainDrew · Sep 30, 2015 at 01:13 AM 0
Share

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.

avatar image abhilash-im · Feb 23, 2016 at 08:47 AM 1
Share

Excellent answer. Unity $$anonymous$$m should have included this in their documentation.

avatar image PigChop · Oct 07, 2016 at 07:09 PM 0
Share

I'm having a hard time understanding what you mean. Could you elaborate?

-PigChop

avatar image laserbahia · Apr 06, 2021 at 12:53 PM 0
Share

Thanks!!! I spent hours searching a solution, saved my day.

avatar image
1

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
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aditya007 · Oct 26, 2016 at 05:01 AM 0
Share

Do these 'value' have any important purpose, or i can just ignore them?

avatar image Develoop · Sep 15, 2017 at 04:44 PM 0
Share

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

alt text

screen-shot-2017-09-15-at-194021.png (78.9 kB)
avatar image
1

Answer by Tarrag · May 17, 2020 at 03:01 PM

five years on and @architrathi answer still works! thanks!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by unimechanic · Oct 06, 2014 at 09:28 PM

You can ask in the Beta group, where our developers could help.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image michellejean · Oct 06, 2014 at 09:31 PM 1
Share

I would be happy too. Could you post a link? Thanks.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Reafi · May 29, 2019 at 07:29 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move gameobject to button in new 4.6? 0 Answers

How to access SourceImage component of a button in new GUI 4.6 1 Answer

How to instantiate a prefab between Canvas and a Button? 0 Answers

If button highlighted 2 Answers

my fadeout in the new GUI system is not working... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges