Getting the button you clicked on?

hi there

situation:
For my Editor Extention, i create buttons in a loop according to a list array of the selected GameObject.
Each click on a button should call a function for the button.

i create the buttons with:

if(GUILayout.button("button label")){call_function();}

problem:
f.e. i want to delete the button from inside the function that is called with the button…
How can i get the button object?

hope for help and thanks for reading :smile:

It’s very unhealthy to remove an element from the loop when within a loop. UnityGUI system doesn’t like that.

You should just mark it as a candidate and delete it later. For instance:

int index = -1;
for (int i = 0; i < buttonList.Count; i++) {
    if(GUILayout.button("button label")){
        index = i;
    }
}

if (index != -1) {
    buttonList.RemoveAt(i);
    index = -1;
}

What i meant is something like this:

[...]
    if(GUILayout.button("button label")){call_function();}
[...]

void call_function(){
    //do something
    delete(this_button);
}

cause i want to run many buttons over the same function on click :confused:

Yes, you should do that - within a loop - but your data (button labels?) should be kept in a list or similar.