I’m currently creating an object pool to reuse objects from in Unity. In the main game manager, I define the stack.
public static Stack baseMissileBulletStack = new Stack();
I then initialize the projectiles:
GameObject newBullet = (GameObject)Instantiate((GameObject) mainGameManager.weapBulletPrefabs.baseMissileBullet, Vector3.zero,
Quaternion.identity);
To remove an object from the stack, I use:
GameObject newProjectile = (GameObject) PlayerBulletPool.baseMissileBulletStack.Pop()
And this is where the error kicks in during run-time:
InvalidCastException: Cannot cast from source type to destination type.
Remove the (GameObject) case and you get this compile error:
Cannot implicitly convert type `object' to `UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)
So basically, I cannot remove an object from the stack without casting it, and Unity doesn’t acknowledge it at run time anyway. Even if I made newProjectile an Object type, there’s no way to convert it into a GameObject. How do I make this work?