0

我在将 a 添加GameObject到 a时遇到问题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; 
    } 
}
4

1 回答 1

3

代替

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

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

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

于 2016-04-04T05:24:27.033 回答