How to name elements of an array?

How would i go about naming elements of an array? I know it would require using the custom editor.

Just the unity tutorial on this is terrible and doesn’t really explain anything useful and other questions don’t have any explaination

Any help would be great

What do you mean name?

Like is it an array of GameObjects, and you want to set the name of each element?

Or do you mean you want the editor to display a title for each element in the array?

Something else all together?

Be specific.

You normally don’t name them. The contents of an array are identified by index.

Are you looking for a dictionary instead?

Simple answer:

Make an array of a struct and include a string parameter named “title” like so:

public MyStruct[] structArray;
[System.Serializable]
public struct MyStruct {
    public string title;
    public int number;
}

The second parameter being the actual value type you want to save in your original array.

1 Like

That should work… If I put that on a class of mine, I get this in the inspector:

3499777--278939--upload_2018-5-16_19-28-54.png

Is that not working for you?

Call it Name and Unity does inspector magic for you.

Yes, but how can this be done without having to dirty up your models unnecessarily?

It was already answered, the best approach is using dictionaries.
Names would be the key and your values would be, well, the value.

The answer using a struct array is only if you really need to see the array on inspector, otherwise there’s no need for it.

Put something like this in the top of your class/struct

#if UNITY_EDITOR
[SerializeField] private string title;
#endif