将GameObject添加到类中列出时出错

时间:2016-04-04 05:19:42

标签: c# unity3d object-pooling

我在向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; 
    } 
}

1 个答案:

答案 0 :(得分:3)

替换

var clone = GameObject.Instantiate (prefabs) as GameObject  ; 

var clone = GameObject.Instantiate (prefabs) as RecyclingGameObjects;  

因为您使用RecyclingGameObjects将列表声明为List<RecyclingGameObjects> poolInstances,所以要添加到列表的对象类型必须是RecyclingGameObjects对象的类型而不是GameObject。

相关问题