unity3d 如何在Unity中创建列表?

jjjwad0x  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(272)

创建列表时,出现以下错误:使用泛型型别system.collections.generic.list'需要1'个型别参数
下面是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyPotionForever : MonoBehaviour
{
    public static List namesOfDestroyedObjects = new List();
    // Start is called before the first frame update
    void Start()
    {
        if(namesOfDestroyedObjects.Count>0){

            for (int i = 0; i < namesOfDestroyedObjects.Count; i++) {
                Destroy(GameObject.Find(namesOfDestroyedObjects[i]));
            }
        }
    }
    void OnTriggerEnter(){
        namesOfDestroyedObjects.Add(this.gameObject.name);
        Destroy(this.gameObject);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
k10s72fa

k10s72fa1#

你需要声明你的列表有哪种类型。例如:

public static List<string> namesOfDestroyedObjects = new List<string>();

顺便说一句你不需要检查:

if(namesOfDestroyedObjects.Count>0){}

在第一次检查for循环时,如果“namesOfDestroyedObjects.Count”不大于0,则for循环将立即中断。

9o685dep

9o685dep2#

在C#中创建列表:

public static List<T> listName = new List<T>();

您必须指定List<>的类型。如List<int>List<Object>。具体取决于您的需要。

相关问题