- Home /
The question is answered, right answer was accepted
Problem declaring List variable in JS
Quick question, I see a lot of people on the forums using lists in Javascript. Everytime I try to declare one to use, I get an error on my console saying that "List" is an unknown identifier. Any ideas why this is?
Example declaration:
var targetList=new List();
I've tried various other syntax from the forums and none of them work.
Thanks, -Kaze-
Answer by AlucardJay · Feb 13, 2013 at 12:16 AM
You have to use this to use List :
import System.Collections.Generic;
this is how to typecast a list of gameObjects :
var inRangeList : List. < GameObject > ;
this is how to declare a list of gameObjects :
inRangeList = new List. < GameObject > ();
Here is a really useful page on arrays and lists (for lists, scroll down to Generic List) : http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?
Thanks for the answer $$anonymous$$, however, I tried importing your first line of code already and I got an error, something like "you must declare the individual generic collections to import." or something like that.
That is most strange. The link I provided shows how to manage all types of lists/arrays/etc, and this is directly from one of my working projects. What version of Unity and OS are you using?
The issue was occurring in Win 7 w/ Unity 3.x and 4. I just recently upgraded to Windows 8 and still have Unity 4. The problem still persists.
Hey, this ended up working! I must've mistyped something before because I'm not getting the error anymore. Will the import need to be done for every script I want to use lists in? Or if the system imports it on one script, can it be used for others?
I wasn't sure as I've been adding it to every script with List, but after a quick test it seems you only need import System.Collections.Generic; on one of the scripts in the scene. (test was 2 scripts on the camera in a new scene).
Answer by T27M · Feb 13, 2013 at 12:17 AM
I also get an error in unityscript try this
var mylist = new System.Collections.Generic.List.<GameObject>();
Thanks for the answer, I'll check this out when I get home.
Updated to the full code if your still having troubles. The list type goes between
Follow this Question
Related Questions
A node in a childnode? 1 Answer
List of objects with limited number availiable? 1 Answer
JS to C# List type problem 1 Answer
How do I use current List Object [i] as the string name? 3 Answers
Far Clipping Range syntax 1 Answer