- Home /
C# lists... again
I just started learning C# and Unity and I'm having trouble with lists and i've searched the forums for similar problems and their solutions. Most suggest adding "using System.Collections.Generic;" fixes the problem.
In the code bellow "List list = new List();" works fine, but all the other lists return "The type or namespace name list
1' could not be found." errors.
Does anyone know what i'm doing wrong? I'm using Unity 4.3. Any help would be great, thanks!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LearningScript : MonoBehaviour {
// Use this for initialization
void Start () {
List<int> list = new List<int>();
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
Debug.Log("This list has " + list.Count + " numbers");
Debug.Log("The list index 0 is " + list[0]);
Debug.Log("The list index 1 is " + list[1]);
Debug.Log("The list index 2 is " + list[2]);
Debug.Log("The list index 3 is " + list[3]);
list<int> myInt = new list<int>();
list<char> myChar = new list<char>();
list<float> myFloat = new list<float>();
list<bool> myBool = new list<bool>();
// list<string> cars = new list<string>();
// cars.Add("Kit");
// cars.Add("Ford");
// cars.Add("Fiat");
// cars.Add("Porche");
// Debug.Log("This list has " + cars.Count + " cars");
// Debug.Log("The car at index 0 is " + cars[0]);
// Debug.Log("The car at index 0 is " + cars[1]);
// Debug.Log("The car at index 0 is " + cars[2]);
// Debug.Log("The car at index 0 is " + cars[3]);
}
// Update is called once per frame
void Update () {
}
}
Do you have a data-structure called list
? I suspect you mean List
with a capital L
.
Answer by Lovrenc · Dec 05, 2013 at 03:23 PM
Case is important! Is it List (with capital L) not list (which in your scope is a variable).
Usually classes are named with capital first letter (e.g. Integer, List, Vector). You can see that your "list" is not even coloured, while the "List" in line 11 is purple. This is one indication that the class was found.
HA! now i feel like a right muppet :) One of those wood for the trees moments. To make it worse i'm using SublimeText as my IDE, so the highlighting doesn't work the same.
Thank you very much! Cheers
Your answer
Follow this Question
Related Questions
Unknown Identifier 'List' 1 Answer
How to use a list to populate a target object? 2 Answers
UNITYSCRIPT - Class's and Instances - Functions/Methods - Correct Usage 0 Answers
Always adds items to list 1 Answer
Class lists not entirely working 1 Answer