Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Eoghan · Dec 24, 2014 at 09:48 PM · uiprefabbuttononclick

Passing through a GameObject/Function to a button's OnClick

I've got a prefab (generic store item) being loaded into a panel, and I'm using a parent gameobject to update all store items with their correct info - item name, image etc etc, which is all simple enough reading/writing from Lists.

However, loading in the button is a little more difficult - I need it to first take in an existing object in the scene (which contains attached scripts that handle the store/item logic), into the OnClick() component. I then need to ensure the OnClick() is set to call the correct function (buyThis(int n)), and also pass through a value for n.

Can anyone point me in the right direction on this?

Edit: For clarity, here's the prefab below:

alt text

So under On Click(), I need to attach this to an object that's not a prefab - but is actually in the scene. Following this, I need to set the actual method that I want to be called, and set the int argument. Here's a manually placed one;

alt text

So, in this one we can see the object attached, the method/function selected, and a value inserted for the required int argument.

In short, when instantiating/placing the prefab into the scene, I need to insert custom values for the gameobject to be loaded, and then for the method & arguments to be updated too.

purchase2.png (22.5 kB)
purchase.png (125.5 kB)
Comment
Add comment · Show 3
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 H.Mueller · Dec 24, 2014 at 10:02 PM 0
Share

I think its not clear, where the button is in this context, maybe yould explain this more deep.

avatar image Eoghan · Dec 24, 2014 at 10:13 PM 0
Share

@H.$$anonymous$$ueller ; updated above, to explain with a little more clarity :)

avatar image Baste · Dec 25, 2014 at 12:36 AM 0
Share

If you put the script that handles the onclick logic on a gameobject that's a part of the same prefab as the button (like the button itself), you should be able to bind that connection on the prefab.

It's still not ideal, but then again, anything else than a script attached to the button handling the onClick logic is not ideal at all.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by wowipop · Dec 26, 2014 at 05:07 AM

     public ACSILogic acsiLogic//get the script in here
     int num;//some number
     GameObject newGO = (GameObject)Instantiate(theprefab);

     UnityEngine.Events.UnityAction action1 = () => { acsiLogic.buyAsci(num); };
     newGO.GetComponent<Button>().onClick.AddListener(action1);//find the button and set


make sure you have a empty OnClick() at your prefab button.. this will not show in the editor.

Comment
Add comment · Show 4 · 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 Eoghan · Dec 26, 2014 at 06:18 PM 0
Share

Added this in, ensured an empty onClick() was available in the prefab button - but unfortunately, no joy. I've tried it with basic Debug.Log calls, and nothing is occuring on a button click. Any other ideas?

avatar image wowipop · Dec 27, 2014 at 03:01 AM 0
Share
 void Start(){
           GameObject newGO = (GameObject)Instantiate(theprefab);
  
      UnityEngine.Events.UnityAction action1 = () => { this.someFunction(); };
      newGO.GetComponent<Button>().onClick.AddListener(action1);
 
 }
 
 void someFunction(){
    Debug.Log("Hello world");
 }


Are you on 4.6.1? cause this work for me.. I think you should remove all OnClick() there too, or wait for some better solution if I found any

avatar image Lichemperor wowipop · Mar 21, 2016 at 12:13 AM 0
Share

This works great, thanks!

avatar image sourav13 · Apr 30, 2016 at 12:48 PM 0
Share

Hello Sir, @wowipop I am facing an issue here i used the below code in a for loop i want to associate a action to a button which is a prefab and i'm having a counter in that for loop for index , what happens is when i associate an action this way each button gets an action but doesn't get the correct index value . It just associate the function to each button with the index value which is the last i.e the number of times loop runs.

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

31 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

Related Questions

Weird behaviour when trying to dynamically create buttons 0 Answers

button.onClick.AddListener(method); NOT Working 1 Answer

Button only works once 1 Answer

Display text above prefabs in Unity 4.6 0 Answers

How to ADD one Buttons.onClick events to another Buttons.onClick? 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