# How to add to a MonoBehavior array from an Editor script in Unity

I've made an editor which makes a list of tags and splits them into two rows of true or false values (selectable by the user). I then compile this list into a dictionary, and each entry is like so:

{"tag", bool, bool}


On my MonoBehavior script, I have two arrays:

string[] depth_tags;
string[] angle_tags;


My goal is to essentially save the dictionary data and be able to recall it/also have it affect the script. To do this, I've been trying to add to these arrays according to the values found in the Editor script's dictionary.

I've done quite a bit of searching and this was the only potential solution I could come up with, but sadly it did not work.

EDIT: Ok, I can now edit the list (added and subtracted from arraySize when doing operations on them), but I have a new problem:

The array reports as being twice as large as it should be. The first half of the arrays are correct and as they should be, but the latter half is made up of empty values. When I try to access beyond the first half, Unity throws an error saying that I exceeded the array's max range.

EDIT 2: FIXED

Answers telling you to increase and decrease arraySize are WRONG/OUTDATED. Clearing, inserting, and deleting from the table does that for you! Quick Tip: Inserting at index 0 in a serialized table takes care of the element bumping for you! No need to cycle through the table and move each array.

void update_tags()
{

SerializedProperty depth_tags = serializedObject.FindProperty("depth_tags");
SerializedProperty angle_tags = serializedObject.FindProperty("angle_tags");

depth_tags.ClearArray();
angle_tags.ClearArray();

foreach (string tag in tag_list_values.Keys)
{

if (tag_list_values[tag][0] == true)
{
depth_tags.InsertArrayElementAtIndex(0);
depth_tags.GetArrayElementAtIndex(0).stringValue = tag;
}

if (tag_list_values[tag][1] == true)
{
angle_tags.InsertArrayElementAtIndex(0);
angle_tags.GetArrayElementAtIndex(0).stringValue = tag;
}

}

serializedObject.ApplyModifiedProperties();

}


}

• "but sadly it did not work" and what did it do instead? The precise symptoms of the problem can be a big help to identifying the right fix quickly. Mar 30, 2019 at 8:03
• @DMGregory it did absolutely nothing to the MonoBehavior script. Tags were all fine and dandy in the Editor script, as well as their respective bool values, but it didn't change the depth or angle_tags lists at all. (It starts empty so maybe it was able to empty it, but my point is that it didn't ADD anything to it) Mar 31, 2019 at 2:25