Question by
piero-trujillo · Mar 27, 2016 at 10:27 PM ·
c#noobobject poolenemy spawn
How To Add Multiple Objects To My Object Pooler?
Hello Unity,
I'm new to scripting and need to add multiple prefabs to my object pooler so I can have a variety of enemies. However, my object pooler only allows for one object.
Script below:
Any ideas on having my object pooler allow more than one object would be much appreciated. Thanks!
screen-shot-2016-03-26-at-103259-pm.png
(23.3 kB)
screen-shot-2016-03-27-at-32101-pm.png
(273.2 kB)
Comment
Best Answer
Answer by fred_gds · Mar 28, 2016 at 12:58 AM
Well just create a second variable like:
public GameObject pooledObject2;
Then change the code inside start to:
for(int i = 0; i < pooledAmount; i+=2)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
//and the two lines that follow
}
for(int i = 1; i < pooledAmount; i+=2)
{
GameObject obj = (GameObject)Instantiate(pooledObject2);
//same two lines you have added above
}
When changing the code like this, every second object inside your pool will be pooledObject2. From here on you should be able to figure out how to add more objects quite easily :)