- Home /
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:
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;
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.
I think its not clear, where the button is in this context, maybe yould explain this more deep.
@H.$$anonymous$$ueller ; updated above, to explain with a little more clarity :)
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.
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.
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?
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
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.