Search Unity

Question Setting An Array Length Before On Start

Discussion in 'Scripting' started by DotArt, Jan 9, 2023.

  1. DotArt

    DotArt

    Joined:
    Jun 12, 2021
    Posts:
    82
    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

    Code (CSharp):
    1.     public bool[] Parts.Length = 5;
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Call
    new
    to make an array of the desired size.
     
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    Yes that was a guess but it wasn't a "good guess" :( 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?
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    You can do

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. public bool[] Parts;
    2. void Reset()
    3. {
    4.     Parts = new bool[5];
    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.
     
    angrypenguin likes this.
  5. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    So essentially... no you cannot limit the array inputs to 5 pre-allocated strings. Is that a fair conclusion?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
  7. Deleted User

    Deleted User

    Guest

    You can do it on `OnValidate` method:

    Code (CSharp):
    1. public string[] myStrings;
    2.  
    3. private void OnValidate()
    4. {
    5.   if (myStrings == null) myStrings = new string[5];
    6.   if (myStrings.Length != 5) Array.Resize(ref myStrings, 5);
    7. }
    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).

    Code (CSharp):
    1. [System.Serializable]
    2. public struct PreallocatedStringArray
    3. {
    4.   public string s0;
    5.   public string s1;
    6.   public string s2;
    7.   public string s3;
    8.   public string s4;
    9.  
    10.   public int Length => 5;
    11.  
    12.   public ref string this[int index]
    13.   {
    14.     get
    15.     {
    16.       switch (index)
    17.       {
    18.         case 0: return ref s0;
    19.         case 1: return ref s1;
    20.         case 2: return ref s2;
    21.         case 3: return ref s3;
    22.         case 4: return ref s4;
    23.         default: throw new System.ArgumentOutOfRangeException(nameof(index));
    24.       }
    25.     }
    26.   }
    27. }
     
  8. Deleted User

    Deleted User

    Guest

  9. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    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.