OnClick() problem with Button script: "Deleting an array element will copy the complete array to all..."

I have six UI buttons with the default OnClick() behaviour. Each button has 5 callbacks which are invoked when each button is pressed, accordingly. Here is the screenshot of 4 buttons next to each other to demonstrate what I am talking about:

As you can see, right now 4 of the callbacks are the same, but with different values passed. The 5th callback is identical for all the buttons.

What I would like to do is to remove the showBoxUI.SetAnimationTo from each button. I would do it by selecting all 6 buttons, clicking the 2nd element and then pressing the minus button to remove it. And while it removes it well from all 6 buttons, I am prompted with the following message:

Deleting an array element will copy the complete array to all… Unique values in the different selected objects will be lost

And when I press the “Delete” button, the 2nd element is removed from all the buttons, BUT all the different values on each button are set to the same value!

Why does that happen and is there a way of preventing it? How do I deal with that? Obviously, I don’t want to re-type each text every time I remove part of functionality from the buttons…

Thank you in advance.

The reason why this happens is because the way Unity handles editing multiple objects.

For instance, when you have a script with a string variable. You put that script on 3 different GameObjects and give them each a different value. Then you select all 3 GameObjects (now you are ‘editing multiple objects’) and when you modify the string. The result is that all strings will be modified.

Now with this array. When ‘editing multiple objects’, Unity doesn’t look at each array item individually, but at the complete array as a whole. So if you then multi edit one item of the array, you are actually modifying the complete array. And therefore, sinse you are multi editing, you are multi editing all arrays.


I think you might be able to overcome this issue by writing a custom instector script. But this is tricky, sinse for instance you have the following setup:

  • 1 array of length 2
  • 1 array of length 2
  • 1 array of length 6

And now you want to modify the item at index 5 of the third array.
But the other 2 don’t have this item at a different index, should they:

  • add it?
  • add index 2 till 5?
  • Modify their own index 1 (as that is their last )?
  • or just copy the entire array as Unity does?

As you can imagine, there is no ‘good’ solution in all situations. So therefore unity chooses to modify the array as a whole which I think is the best approach.

Also, sinse you are using the builtin Unity button and not a custom class for which you could be able to build a custom editor, I think you best (and easiest) option would be to simply not multi edit (i.e. remove the second item from one button at a time)…


Another (ugly) workaround could be something like this…

public class SomeExtraFunctionality : MonoBehaviour
{
	public UnityEvent myEvent;

	public void Start()
	{
		GetComponent<Button>().onClick.AddListener(myEvent.Invoke);
	}
}

If you add this script to the button, even though the event on this script has the same issue. If you add ‘item 2’ to this script and the other items to the button, you could multi edit it without modifying the array on the button. Not really a solution to the problem, but depending on your situation it might be helpfull…