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>();
1 Like

2 Answers

2

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;

Thank you for this great explanation :)

–

Did you guys manage to use this with MonoDevelop's debugger? when trying to see the contents of the Lists in the Locals inspector, it shows as "Unknown Type ' System.Collections.Generic.CollectionDebuggerView'" Nevermind, found the answer here: http://stackoverflow.com/questions/14329222/unity-weird-debugging

–

Yeah, neither of the solutions work for me. It underlines the "=" and "(" in red and give the error "Invalid token '=' in class, struct, or interface member declaration" for each

–

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

C# List examples … C# List