0
\$\begingroup\$

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();

}

}

\$\endgroup\$
  • \$\begingroup\$ "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. \$\endgroup\$ – DMGregory Mar 30 '19 at 8:03
  • \$\begingroup\$ @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) \$\endgroup\$ – gmanicus Mar 31 '19 at 2:25
1
\$\begingroup\$

The property's array size needs to be edited every time (or appropriately before) an operation is made.

WRONG. Not sure what I changed to make it work, but I laid out my code in the original question.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Then please mark your question as answered. \$\endgroup\$ – nihohit Mar 31 '19 at 3:34
  • 1
    \$\begingroup\$ @nihohit Have to wait a day to accept my own answer. \$\endgroup\$ – gmanicus Mar 31 '19 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.