Unity Button Script
Hello, i wanted to know if there is any ways to fill a function and the parameter of it without using AddListener :
i used an AddListener for my script currently but i believe that if i fill in these by sript, it would be more efficient, thank you for your responses ! :)
I don't understance what you are trying to achieve.
The OnClickEvent of a button can be assigned using the inspector (drag & drop the gameobject and choose the function), or by script using the button.onClick.AddListener( listener )
. There is no other way.
I basically instatiate a gameobject and want to give it an onclick event, but when the "button" is clicked, i'd like it to pass in the gameobject as a parameter so i can use it in my script
i can go
AddListener( delegate { method(parameter); )
but i would have just liked to know if there are any ways to add the button via script, then fill in the different slots on the "OnClick()" manually... anyways if you say there's no other way, thank you :)
Oh, I see now.
It's possible to do so in the inspector but only for public functions "returning" void
with only one parameter which is either an int, float, bool or string I think.
Otherwise, you will have to do it by code indeed. But I may be wrong, maybe, some 3rd parties plugins focused on Serialization (such as Odin) can do this without code.
Yes, the only problem is that i don't know how to set the value for an onlick event from the script... i want to basically do the same thing as in the inspector ( add an object -> get the component -> give it a function with parameter ) so when i click on it, it would return a value. As i said, i used AddListener( delegate { method(parameter); ) but it doesn't seem to be working, i tried searching and in found this link text
Now, so let me explain :) i have different "Equipment" and in a function, i try to find the slot index of an "Equipment" : public void AddEquipmentSlotUI(Equipment equipmentAdd, int slotIndex) { if (slotIndex == 0) { newEquipmentSlotBox = Instantiate(equipmentSlotBox, transform.position, transform.rotation);
// as i read in the link above, i made another GameObject and set its value to the newEquipmentSlotBox so i could try and get its correct value everytime
GameObject newGO = newEquipmentSlotBox;
// this is where i want to add a button --> with "addListener"
newEquipmentSlotBox.GetComponent<Button>().onClick.AddListener(delegate { RightEquipmentAdd(newGO); });
}
}
What happens everytime i "click" on one of these Instantiated gameobjects is that it doesn't return the correct "Equipment" of the correct "slotIndex", this means that when i add the "AddListener", the "newGO" is always = to the LAST equpment i added to my list
Your answer
Follow this Question
Related Questions
activate button on triggerenter 0 Answers
Spawn prefab on UI button click 0 Answers
I want to press a Button and play a sound? 2 Answers