I am creating an inventory UI and I am displaying my items in a scrollable list. Each UI element in this list has a button component and is instantiated during runtime. When I click the item in the list I have a menu that pops up giving options as to what to do with the item. I have attached a container script with all of the items basic data to the same UI element with the button so I don’t have to keep iterating through my item database each time I want to reference a specific item. When I click the button I want to be able to store the gameObject the button component is attached to for future use. How can I achieve this?
Also if anyone knows, how can I make the menu that pops up when I click the button move to the position of the mouse?
I haven’t tried this before, but [UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject](https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem-currentSelectedGameObject.html) might get you what you need, if called during the button event handler.
Note: The given approach is useful, when you need dynamicly show the clicked buttons name, but if you know the exact button, you can make a public field for the myButton and get the name without passing any parameters.
I’m currently using a slightly different method, which is to request a parameter of type GameObject when the button calls the method. Then, I link the script to the OnClick() field in the Button → select the correct method and link the GameObject of that Button.
public void Checker_DebugLog(GameObject button)
{
Debug.Log($“THIS BUTTON IS WORK - {button.name}”);
}