You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

How to create and use array in Unity using C#

How to create and use array in Unity using C#

Ben Liu
Ben Liu

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.

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp