我在向GameObject
添加List<>
时遇到问题。当我构建此程序时,poolInstances.Add(clone)
:
错误:List.Add(RecyclingGameObject)有一些 无效的论点。
return clone;
行也出现错误:
错误:无法将Gameobject隐式转换为RecyclingGameObject
这是我的代码:
using System.Collections;
using System.Collections.Generic;
public class ObjectPool : MonoBehaviour {
public RecyclingGameObjects prefabs;
private List<RecyclingGameObjects> poolInstances = new List<RecyclingGameObjects();
private RecyclingGameObjects createInstance(Vector3 pos){
var clone = GameObject.Instantiate (prefabs) as GameObject ;
clone.transform.position = pos;
clone.transform.parent = transform;
poolInstances.Add (clone);
return clone;
}
}
答案 0 :(得分:3)
替换
var clone = GameObject.Instantiate (prefabs) as GameObject ;
与
var clone = GameObject.Instantiate (prefabs) as RecyclingGameObjects;
因为您使用RecyclingGameObjects
将列表声明为List<RecyclingGameObjects> poolInstances
,所以要添加到列表的对象类型必须是RecyclingGameObjects
对象的类型而不是GameObject。