Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to declare a generic list with elements in it ? JS

Discussion in 'Scripting' started by SmokyZebra, Apr 23, 2014.

  1. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Hello, i would like to replace some arrays with generic list but i can't find examples of a generic list being declared and that have elements in it.

    So at the moment, i have arrays, declared like this :

    Code (csharp):
    1. static var namesShipsImperium : Array = new Array( "Seduction Star", "Flame Of Theory", "Lord Inquisitor");
    All i could find on how to declare a list was this :

    Code (csharp):
    1. import System.Collections.Generic;
    2.  
    3. var namesShipsImperium = new List.<String>();
    4. namesShipsImperium[0] =  "Seduction Star";
    5. namesShipsImperium[1] =  "Flame Of Theory";
    6. namesShipsImperium[2] =  "Lord Inquisitor";
    So is it possible to declare a list with those elements in a single line, as it is possible for arrays ? If yes, please show me how to do it.
     
  2. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    So, nobody knows or that's just impossible ? Please help, I really need this information to optimize the code of my project...
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var namesShipsImperium = new List.<String>(["Seduction Star", "Flame Of Theory", "Lord Inquisitor"]);
    --Eric
     
  4. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Thanks a lot !