C# Array doesn't Assign when Script Starts

HI everyone, I have encountered a problem with arrays.If I declare public string someArray = new string[4]; it doesn’t set someArray to 4 elements. What can I do to make someArray equal to 4 string elements when the script starts?

How have you determined that it hasn’t created 4 elements? The array is actual of length 4 when you declare it that way, but you have to actually put something in each array position if you want contents, otherwise each position is null.

If you know the values up front you can declare the array like this to initialize the values:

public string[] someArray = { "Value 1", "Value 2", "Value 3", "Value 4" };

Otherwise you will need to fill in each value…

someArray[0] = "Value 1";
someArray[1] = "Value 2";
... and so on ...

You can change size of array in editor. Because unity override your values.Or
void Awake(){

someArray = new string[4]


you can do this in Awake,Start or Update functions.

Public variables are set in the inspector, so don’t use code like that for declarations. It should just be

public string[] someArray;

If you want it to always be 4 elements, then it probably shouldn’t be public, and should instead be a private array that’s initialized in Start or Awake.