Setting An Array Length Before On Start

Hey, i was wondering if theres a way to set the length of an array so in the inspector it is shown as that length without having it start at 0 and then having to add how many myself, i know you can do on start and make the length = to what you want but was wondering if theres a way to set it in the inspector. I tried something like this but that didnt work ahaha

    public bool[] Parts.Length = 5;

Call new to make an array of the desired size.

Yes that was a guess but it wasn’t a “good guess” :frowning: As far as I know you can define the length in your code but this isn’t going to change the way the inspector handles arrays. Why not just add array elements in the inspector as needed? How often do you do this?

You can do

public bool[] Parts = new bool[5];

However I would not necessarily recommend that. This would always create that array when the class is created. However since the field is serialized, it would be replaced by the deserialized version anyways. So all it does is generate extra garbage every time the object is created / instantiated.

A slightly better approach would be to use the Reset method like this:

public bool[] Parts;
void Reset()
{
    Parts = new bool[5];
}

Though note that the Reset method is only called once when you create an instance in the editor or when you select “Reset” from the context menu. After the object has been created and is serialized, only the values serialized are relevant. So you can still change the length of the array in the inspector so it contains more or less elements.

1 Like

So essentially… no you cannot limit the array inputs to 5 pre-allocated strings. Is that a fair conclusion?

It might be possible if you implement this:

https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnAfterDeserialize.html

That way if you ever see it as anything you don’t like, you can fix it… I think.

Caveat: I’ve not used these callbacks personally, but the issues they address seem like they might be suited to this sort of “Make sure I only have X of these things always.”

You can do it on OnValidate method:

public string[] myStrings;

private void OnValidate()
{
  if (myStrings == null) myStrings = new string[5];
  if (myStrings.Length != 5) Array.Resize(ref myStrings, 5);
}

or if you want them pre-allocated, the only way to do that in C# is to create a new struct (you can get fancier with this if you use unsafe pointers to the struct instead of a switch statement).

[System.Serializable]
public struct PreallocatedStringArray
{
  public string s0;
  public string s1;
  public string s2;
  public string s3;
  public string s4;

  public int Length => 5;

  public ref string this[int index]
  {
    get
    {
      switch (index)
      {
        case 0: return ref s0;
        case 1: return ref s1;
        case 2: return ref s2;
        case 3: return ref s3;
        case 4: return ref s4;
        default: throw new System.ArgumentOutOfRangeException(nameof(index));
      }
    }
  }
}

Wanted to add one more thing. Seems like Unity has added support to serializing fixed size buffers. Here’s how you can use them: C# Fixed Size Buffer - Dotnet Stuff

All things are fun but if the purpose is to avoid pressing the inspector button to add array elements I’d caution against doing anything too outlandish.