Hey guys,
I was watching the live training for Object Pooling by Unity which was for only one object/prefab. I’ve tried tweaking the script mentioned in the video but I’ve got errors.
Here is my script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObjectPoolScript : MonoBehaviour
{
public GameObject [] pooledObject;
GameObject platformPrefab;
public int pooledAmount = 20;
public bool willGrow = true;
public List<GameObject> pooledObjects;
void Start ()
{
platformPrefab = pooledObject[Random.Range(0, pooledObject.Length)];
pooledObjects = new List<GameObject>();
for(int i = 0; i < pooledAmount; i++)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
for(int i = 0; i< pooledObjects.Count; i++)
{
if(pooledObjects *== null)*
-
{*
-
GameObject obj = (GameObject)Instantiate(pooledObject);*
-
obj.SetActive(false);*
_ pooledObjects = obj;_
_ return pooledObjects*;
}
if(!pooledObjects.activeInHierarchy)
{
return pooledObjects;
}
}*_
* if (willGrow)*
* {*
* GameObject obj = (GameObject)Instantiate(pooledObject);*
* pooledObjects.Add(obj);*
* return obj;*
* }*
* return null;*
* }*
}
Here are the errors:
1)error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object)' has some invalid arguments.*_</em></em></em> <em><em><em>_*2)error CS1503: Argument
#1’ cannot convert UnityEngine.GameObject[]' expression to type
UnityEngine.Object’
Both errors happened to lines (20,54),(32,62) and (45,54).
Anyone could point out my mistakes? Also, I was wondering is it possible to make the pooling script also a spawning script? As in objects/prefabs spawning out from an empty GameObject which contains the pooling script.Thanks in advance! =)