How to create and use array in Unity using C#

How to create and use array in Unity using C#

· 1 min read

Features of builtin array in Unity

  • Fixed size once it is created, which means you can't add items to it or remove items from it
  • All elements are the same type
  • Type safe, program will read the data from memory in the way it was originally stored.
  • Efficient in terms of memory and performance (the memory address are contiguous)
// define an empty array
float[] values;

// define an array length equal 5
int[] values = new int[5];

// define a 3-demension array
int[,,] values = new int[10,20,30];

// define an array with values
int[] values = new int[] {1, 2, 3, 4}

// get the array length
Debug.Log(values.Length);

If you want an array which has a flexible length, you can use List and ArrayList.