Get value in button OnClick()
Hello,
I'm developing an RTS which has buildings that the player has to first unlock before they can be placed. I'm alright with having the buttons that construct those buildings visible but I want them to be non-interactive. The way I'm using the buttons is that in each I use the OnClick() to call a function in a script and pass the gameobject to be constructed, which I set in the inspector.
Basically I want to have a button manager script where I check for each button whether the gameobject assigned has a specific parameter, is it possible for me to do that?
you mean: "iterate all buttons, get their assigned gameobjects and check for parameters"? if so, what exactly do you mean with parameter? methods have parameters.
I mean, iterate through a public array of buttons which I will assign from the inspector, and get the gameobject that's assigned to be sent to the function that's called when OnClick() happens.
Basically, my function is defined as setItem(gameobject g), and is part of a BuildingPlacement script which is a component of a Player gameobject. I've assigned the Player to the buttons in question, selected the function mentioned above from the list of functions in the script mentioned above, and assigned a gameobject as a parameter for that function. What I would like to do is get that gameobject for every button I specify.
Answer by hexagonius · Feb 25, 2017 at 03:42 PM
ok. the should be:
foreach (var b in buttons){
GameObject target = b.ButtonClickedEvent.GetPersistentListener(0) as GameObject;
Debug.Log(target.name);
}
this works for one receiver on each button. if there's more you'd need to loop the function.
Thank you, however I'm getting "ButtonClickedEvent cannot reference a type through an expresion", and "Button.ButtonClickedEvent does not contain a definition for GetPersistentListener". I have another idea as to how to do what I want even though it's less dynamic, but still curious to see if it's possible to get the parameter that a button will send to a function if clicked.
Your answer
Follow this Question
Related Questions
unity 2018.3.12f1 Button OnClick call function on another instance? of script 0 Answers
UI Buttons without blocking raycast 0 Answers
How do I make a UI Buttion play an Animation when pressed? 0 Answers
AddListener not working after build 0 Answers
Trying to increment a variable in a Coroutine on pressing a UI button 0 Answers