Lists (C#)

I’ve been trying to create a list, and even when I just copy/paste the variable declaration from someone’s code here on the Answers or the forums I get this error:

The type or namespace name ‘List 1’ Could not be found. Are you missing an assembly reference or directive?

Which doesn’t make sense because people used the exact same code I did and didn’t get this error. What am I doing wrong here?

public List<Transform> myList = new List<Transform>();

Just add:

using System.Collections.Generic;

at the top. See the MSDN docs for more information.

edit
alternatively you could always use the full classname, however that looks a bit strange:

public System.Collections.Generic.List<Transform> myList = new System.Collections.Generic.List<Transform>();

That’s why the using directive has been invented.

edit
Just in case a UnityScript user comes across, in UnityScript (Unity’s Javascript) you have to use the “import” keyword instead:

import System.Collections.Generic;

List iList = new List();
iList.Add(2);
iList.Add(3);
iList.Add(5);

C# List examples … C# List