- Home /
Instantiated object from queue not moving
Hi everyone! I'm working on an incredibly simple 2D game that, for now, involves some blocks and collisions between the blocks. I have the Player block and an "Enemy" block that could come in from either way (right or left). I'm using queues to control the instantiation and "destruction" of objects, every enemy has a movement script attached to it, and there are 5 different spawn points. The code for Queue management:
void InitalizePools()
{
rightPool = new Queue<GameObject>();
leftPool = new Queue<GameObject>();
for (int i = 0; i < rightPoolSize; i++)
{
GameObject newGO = Instantiate(rightEnemyPrefab);
newGO.name = "Right Enemy";
newGO.SetActive(false);
rightPool.Enqueue(newGO);
}
for (int i = 0; i < leftPoolSize; i++)
{
GameObject newGO = Instantiate(leftEnemyPrefab);
newGO.name = "Left Enemy";
newGO.SetActive(false);
leftPool.Enqueue(newGO);
}
}
public GameObject InstantiateEnemyLeft(Vector3 position)
{
GameObject enemy = leftPool.Dequeue();
enemy.transform.position = position;
enemy.transform.rotation = Quaternion.identity;
enemy.SetActive(true);
return enemy;
}
public GameObject InstantiateEnemyRight(Vector3 position)
{
GameObject enemy = rightPool.Dequeue();
enemy.transform.position = position;
enemy.transform.rotation = Quaternion.identity;
enemy.SetActive(true);
return enemy;
}
public void ReturnEnemyToLeftPool(GameObject go)
{
go.SetActive(false);
leftPool.Enqueue(go);
}
public void ReturnEnemyToRightPool(GameObject go)
{
go.SetActive(false);
rightPool.Enqueue(go);
}
The spawning is controlled in the Game Manager by:
void Update () {
timeElapsed += Time.deltaTime;
if (timeElapsed > timeBetweenSpawns)
{
EnemySpawner();
}
}
void EnemySpawner()
{
float side = Random.Range(0f, 1f);
if (side <= rightSideSelectionPercentage)
{
int spawnPoint = Random.Range(0, rightSpawnPoints.Length);
GameObject enemy = PoolManager.instance.InstantiateEnemyRight(rightSpawnPoints[spawnPoint]);
timeElapsed = 0f;
}
if (side > rightSideSelectionPercentage)
{
int spawnPoint = Random.Range(0, leftSpawnPoints.Length);
GameObject enemy = PoolManager.instance.InstantiateEnemyLeft(leftSpawnPoints[spawnPoint]);
timeElapsed = 0f;
}
}
Enqueuing is controlled by the collision with the player:
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
if (gameObject.name == "Right Enemy")
{
PoolManager.instance.ReturnEnemyToRightPool(this.gameObject);
}
if (gameObject.name == "Left Enemy")
{
PoolManager.instance.ReturnEnemyToLeftPool(this.gameObject);
}
}
}
Everything is working as it should, except for some spawn points that on their second call (the first time the enemy spawns there it moves and acts correctly) the instantiated object appears at the spawn point but is not moving. The appropriate script is attached to it and enabled but it won't move. From there, all instantiated objects at that spawn point work but the second spawn remains still at the spawn point. This is instantiated enemies #2 and #3, you can see that 2 is at the spawn point while 3 spawned and started moving (I apologize for the wonderful color scheme, it's for testing purposes):
Every play I run, it's a different spawn point(s), and I have no idea why. I would appreciate any help, thanks in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220612182851im_/https://answers.unity.com/themes/thub/images/avi.jpg)