Search Unity

Arrays or List

Discussion in 'Scripting' started by Jorhoto, May 22, 2019.

  1. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    26
    Hello, I am new to Unity.
    I was wondering if in general is a good practice to favor arrays over lists. So in situations where both would do the job, use always arrays.

    Thank you ;)
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    932
    No.

    You're welcome ;)
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    932
    Jorhoto likes this.
  4. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    26
    Ok, thanks for the info ;)
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    932
    Besides the points listed in the link (I beat you by a couple of seconds :) ) there are so many other things in video games that are orders of magnitude bigger in consuming cycles than variable access that you should not worry about low level stuff unless you really, absolutely, positively, one hundret percent must have the last cycle spared. Because using a profiler, you'll find 1000 other, better suited placed to tweak performance than using arrays over lists when both would do the job.
     
    Jorhoto likes this.
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,662
    List is basically a wrapper class around the array. List have internal array and resize it using optimal resize stategy when you add new items. When you need resizeable array, use list. And if you need only fixed size array, you may stick with array. There's no any noticeable benefits or losses using list or array.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    7,259
    Unless you have a good reason you need an array, generally a list is a better choice as it is more flexible. It is also possible to convert between the two if needed.
     
unityunity