不能在Unity, c#中使用ArrayList



我尝试使用ArrayList来存储一个值,但我不知道为什么会出错。

using UnityEngine;
public class Example : MonoBehaviour
{
ArrayList aList = new ArrayList();
// Start is called before the first frame update
void Start()
{
int i = 3;
aList.Add(i); //error here
Debug.Log(aList[0]);  //and here
}
// Update is called once per frame
void Update()
{
}
}

当我在Unity中创建一个c#脚本时它有using System.Collectionsusing System.Colections.Generic但是当我使用Ctrl+S保存时它们会消失因为IDE005: Using directive is unnecessary

下面是错误语句:

Error CS1061 'ArrayList' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'ArrayList' could be found (are you missing a using directive or an assembly reference?)

我不知道我错过了任何using指令或什么

这实际上是什么类?假定它不是System.Collections.ArrayList类,因为它确实有Add方法。如果这是您打算使用的类,那么您需要确保已经导入了名称空间,否则必须限定类型名称。也就是说,您可能根本不应该使用ArrayList类。不确定它是否适用于Unity,但在。net开发中,该类在2005年被List<T>有效地取代。

尝试更新你的Unity和Visual Studio,下面的代码对我来说工作得很好。(同时确保你在Visual studio中下载了Unity库,这样你就可以得到更好的智能感知建议)

最后一件事,如果你对类有问题,那么试着隐式声明你的类

System.Collections.ArrayList a = new System.Collections.ArrayList();
a.Add("test");

最新更新