Spawning random prefabs using pooling method?

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);
	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*;
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! =)

The Instantiate method requires a UnityEngine.Object as parameter. (Or anything that derives from it like a GameObject) You try to give an array of GameObjects as a parameter.

And well, an array of GameObjects is not a GameObject.

I think you actuly want to instantiate the ‘platformPrefab’ instead of the ‘pooledObject’.

If you for some reason however do want to instantiate ONE OF THE things inside the pooled object, you’ll need to specify which of the GameObjects inside pooledObject you want to Instantiate, by specifying the index like so: ‘pooledObject[index]’ in which index is a value between 0 and pooledObject.length…

If you want to instantiate all GameObjects inside pooledObject, you’ll need to loop through them and call Instantiate for each GameObject inside pooledObject.

I can’t really give you any script example, because I actually have no idea which thing(s) you even want to Instantiate :stuck_out_tongue:

Depending on how different your prefabs are, another way of doing this could be to have only one pool of gameobjects, and assigning the unique values of a random prefab to it when you retrieve a gameobject from the pool, this will reduce the amount of resources you are using compared to the multiple pools option, but might be slightly slower compared to having multiple pools. As though we don’t have to instantiate a gameobject every time we retrieve one from the pool, we do have to set some of it’s values every time.