- Home /
Passing Object Reference to Button
In my script i want to pass the gameObject which contains it to the Object reference in a Button's OnClick event.
is it possible to do that? I looked through the Button page and couldnt find anything mentioning the Object reference and it seems the AddListener method can only pass other methods.
I don't really get what you are trying to do....
You can't "add a reference to an object" to the onClick
event. You add a function (callback). You can add a function defined on any object you want as far as you have a reference to the latter.
public Weapon Weapon; //supposing you have a script called Weapon
public Button button;
void Start()
{
button.onClick.AddListener( () => Weapon.Fire() ) ;
}
I have this barebones right click menu, when i click one of the buttons on it i want to do something with the UI object i just right clicked, there might be more than one such object so i figured the best way to make sure the code has the right object is somehow passing that object to the Button component, as if i dragged the object in the editor.
A few things i forgot to mention, everything is in the same script, the right click menu stuff and the function i want the buttons to call as well, so on the editor i dragged the prefab of the UI element that receives the right click, what i need is that when i right click, that specific object that i right clicked is sent to the buttons somehow and replaces the prefab.
same here, I don't get it.
As far as I understand, you have a menu with buttons (maybe a prefab). There can be more than one instance of that prefab. Each menus buttons should do something with their menu. If that's the case, from the buttons, call methods that reside in their menu on some script. This way every method call you do with a button works on THAT menu...
Or am I missing somethin here?
Its pretty much that, but i want to avoid creating custom scripts for the buttons if possible. And i should've posted this image right away sorry about that, i think i forgot about it.
I want the buttons in that little right click menu to call the equip and destroy functions on that "Axe" object, which contains all the item information itself. Naturally there might be more than one item object, so i need the buttons to work on that exact object.
Answer by Jordash · Nov 01, 2018 at 12:05 PM
If I'm understanding correctly you can solve this by having the item which instantiates the menu pass a reference to itself to the new menu object.
This can be a reference to the GameObject or the script type, but I would suggest the script because you're likely to want access to the functions of the script, and the script can call its game object with more certainty that a game object getting the script.
With a bit more detail, you will need a public variable in the menu script which is the type of your item script. When the item instantiates the right click menu, it can follow that up by setting this public variable to itself. Something like:
GameObject rightClickMenuInstance = Instantiate(rightClickMenuPrefab);
rightClickMenuInstance.itemRef = this;
Then in the button onClick of the menu you can do whatever you wanted to do to the original item.
Have a look at inherited classes or interfaces in the C# documentation if you want to have multiple scripts which all can all conform to a common type.
Answer by Vega4Life · Nov 01, 2018 at 04:49 PM
Just create a function in your script that requires a GameObject parameter (for the button you want to pass to it). The script needs to be on the object you are referencing for the OnClick() event (which is a weapon object in your case). Then, set the OnClick() reference to the object with the script (a weapon object). Then click on the "No Function" drop down, find your script, then find your function callback. Next, drag the button object into the GameObject reference that the callback requires.
The function in the script on the weapon would look like this:
public void ScriptThatIsListening(GameObject go)
{
// Do something with the ref to button
}
Your answer
