- Home /
c# using Lists and IEnumerator's in the same class?
I'm an AS3 programmer and im just getting into Unity so i'm sorry if this is a stupid mistake on my part :p
At the top of the standard c# class that extends MonoBehavior we have the following
using UnityEngine;
using System.Collections;
public class ClassName : MonoBehaviour {
When i tried to use a list like so
private List<string> animationNames;
I got an error saying that the type or namespace 'List' could not be found! After a bit of googling i came across an article that explained I needed to change the top to the following
//using System.Collections;
using System.Collections.Generic;
This worked great, I'm a little confused as to why that fixes it as in actionscript if you import a level up it imports all classes in folders?
Anyway, this worked ok for a while, then i got onto Courtine and Yeilds, which return IEnumerator, which now doesnt work because of the 'using' change. and if i change it back List's break. im sure that this is just a schoolboy error on my part...
Any help would be greatly appreciated and thanks in advance!
Answer by dukearena · Nov 22, 2012 at 10:07 AM
IEnumerators need System.Collections, List needs System.Collections.Generics.. This is not for Unity but it is a C# rule, you must choose what library are necessary for your script..
You can add infinite number of imports statemets like:
using UnityEngine;
using System.Collections;
using System.Collections.Generics;
using System;
using System.Text;
.. and others..
If you have some problem with a class (like List) you can find a good documentation (and what import needs) in MSDN online ( http://msdn.microsoft.com/it-it/library/6sh2ey19.aspx )
I hope your question is satisfied :D
Ps. this is the basic of C#, I recommend you to take some c# tutorial
Thanks dukearena, I honestly dont know why I didn't try this. I understand you can import multiple things. It was my understanding (from AS3) that using System.Collections would import everything within there including System.Collections.Generic. I am looking into getting some books on c# and unity, but all the unity books are about 3.5 as 4 is a newish release, so i think im going wait a bit!
No problems, this mistakes happens a lot of times for me :D You only need some experience
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why is my Mathf.Movetowards Coroutine executing instantly? 2 Answers
Coroutine gets stuck randomly? 1 Answer
Yield not working properly 1 Answer