Populating Unity Event via Script, Help Needed

Hello everyone,

I have a UI element (I’ll call it panel) that shows related stats to an item. I have a method that consumes an integer which is the item ID, and displays the stats.

public void PopulatePanelModulesUI(int ID)

Now this time I will be instantiating a UI element which displays the image of a module, for all modules that the player currently has active. What I want to do is, when the player puts their cursor above the UI image of that module I want to populate the panel by calling the method above.

7652530--954313--upload_2021-11-12_19-33-14.png

Normally I would drag my game object to the left that contains the PanelScript. From the PanelScript I would choose the PopulatePanelModulesUI and in here even enter the ID that I want to pass through.

How can I achieve this via the script since I will be instantiating the module images as the player picks up modules? I have seen a lot of forum posts about how to create Unity Events but I don’t understand how do you choose ALL the parameters I described above? I wish I could just find the game object, then access the script and then say “From PanelScript, call PopulatePanelModulesUI and pass the the ID X

Has anyone done something like this before? Any help is appreciated :slight_smile:

All day, every day… but the key is you won’t SEE the programmatically-added delegate in the inspector. This is due to the limitations of what the editor inspector can show you, which is only a small subset of what can be added listener-wise.

What you want is this:

https://docs.unity3d.com/ScriptReference/Events.UnityEvent.AddListener.html

And here’s some important notes about doing it in a loop:

Delegate / Action variable capture / value capture:

https://discussions.unity.com/t/809602/2

1 Like