Reputation: 15
I am having issues in adding a GameObject
to a List<>
. When I build this program an error occurs in poolInstances.Add(clone)
:
Error: List.Add(RecyclingGameObject) has some invalid arguments.
Error also occur on return clone;
line:
Error: Cannot implicitly convert Gameobject to RecyclingGameObject
Here is my code:
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;
}
}
Upvotes: 0
Views: 54
Reputation: 125315
Replace
var clone = GameObject.Instantiate (prefabs) as GameObject ;
with
var clone = GameObject.Instantiate (prefabs) as RecyclingGameObjects;
Because you declared the list as RecyclingGameObjects
, with List<RecyclingGameObjects> poolInstances
, the object type to add to the list must be type of RecyclingGameObjects
object not GameObject.
Upvotes: 3