Can I change which GameObject is being drawn from a pool via a public variable/the property inspector?
Hey all,
I'm moving away from using instantiations for GameObjects, but there's one thing that I've been puzzling over the last couple of days.
I'm developing a shoot 'em up in 2D, and currently trying to use pools to store bullets and enemy defeat animations (like explosions and death animations). It's possible to attach different types of pods to the sides of your character that fire bullets drawn from different pools. Currently, the main character and the pods use the same script for firing.
To differentiate between the type of bullet each pod fires, I've use a public float and changed the value in the property inspector for each prefab. Currently my firing script contains this (each pod fires two different bullets):
if (shotType == 1)
{
shotTransform = ObjectPool.mainShotInst1.GetmainShotObj1();
shotTransform2 = ObjectPool.mainShotInst2.GetmainShotObj2();
}
if (shotType == 2)
{
shotTransform = ObjectPool.sideShotInst1.GetsideShotObj1();
shotTransform2 = ObjectPool.sideShotInst2.GetsideShotObj2();
}
if (shotType == 3)
{
shotTransform = ObjectPool.sideShotInst1.GetlaserObj1();
shotTransform2 = ObjectPool.sideShotInst2.GetlaserObj2();
}
if (shotType == 4)
{
shotTransform = ObjectPool.sideShotInst1.GetmultiObj1();
shotTransform2 = ObjectPool.sideShotInst2.GetmultiObj2();
}
shotTransform.transform.position = new Vector2(transform.position.x + offset.x, transform.position.y + offset.y);
shotTransform.SetActive (true);
shotTransform2.transform.position = new Vector2(transform.position.x + offset2.x, transform.position.y + offset2.y);
shotTransform2.SetActive (true);
While this works, my game is going to have way more pods, meaning that the shooting script is going to get very complicated very quick.
Similarly, enemies use a HP script which draws a 'death' GameObject from the pool when their health reaches zero - something like an explosion or in most cases a unique death animation. However, since I'm going to have a ton of enemies, I don't know if the method I use above will be viable, since the script will probably run through about 50 'if' statements whenever the enemy dies (one statement for each type of enemy!).
This was all simple enough when I instantiated a public GameObject, since all I had to do select the desired object in the property inspector for each prefab, but I can't find a way to do anything similar for an Object Pool.
Is there any way to be able to change which objects are being drawn out of the pool using a public variable? The type of object to be drawn needs to be changed on the fly as well. Or is the best solution to hard code each script for each different enemy and pod? Ideally I would like the game to run on mobile too so I want to pick ideally the fastest solution.
My coding experience is pretty limited and this is my first post here, so please let me know if my question is too vague (since admittedly I'm at a loss to describe it) or if it would be better suited to a forum post. Thanks!
Your answer
