I’m fairly new to unity/c# and I’m creating a small endless runner game where I use object pooling for the enemies. However I keep running into an issue where my enemies go back into the queue almost as soon as they are spawned. The image below shows the extent of their movement:
I assume I have either somewhere defined how far they can go or I have failed to do so and I’m not sure how to go about doing it. Below are the scripts that may be relevant.
ObjectPooler.cs
public class ObjectPooler : MonoBehaviour
{
public float speed;
[System.Serializable]
public class Pool
{
public string tag;
public GameObject prefab;
public int size;
}
public Pool poolt = new Pool();
#region Singleton
public static ObjectPooler Instance;
private void Awake()
{
Instance = this;
}
#endregion
public List<Pool> pools;
public Dictionary<string, Queue<GameObject>> poolDictionary;
// Start is called before the first frame update
void Start()
{
poolDictionary = new Dictionary<string, Queue<GameObject>>();
foreach (Pool pool in pools)
{
Queue<GameObject> objectPool = new Queue<GameObject>();
for (int i = 0; i < pool.size; i++)
{
GameObject obj = Instantiate(pool.prefab); // Creates an object by grabbing the prefab
obj.SetActive(false);
objectPool.Enqueue(obj);
}
poolDictionary.Add(pool.tag, objectPool);
}
}
public GameObject SpawnFromPool (string tag, Vector2 position, Quaternion rotation)
{
if (!poolDictionary.ContainsKey(tag))
{
Debug.LogWarning( tag + "Doesnt exsist");
return null;
}
GameObject objectToSpawn = poolDictionary[tag].Dequeue();
objectToSpawn.transform.position = position;
poolDictionary[tag].Enqueue(objectToSpawn);
return objectToSpawn;
}
}
CubeSpawner(MainSpawner)
public class CubeSpawner : MonoBehaviour
{
public GameObject[] enemyPatterns;
ObjectPooler objectPooler;
private float timeBtwSpawn;
public float startTimeBtwSpawn;
public float decreaseTime;
public float minTime = 1f;
void Start()
{
objectPooler = ObjectPooler.Instance;
}
// Update is called once per frame
void FixedUpdate()
{
if (timeBtwSpawn <= 0)
{
int rand = Random.Range (0, enemyPatterns.Length);
timeBtwSpawn = startTimeBtwSpawn;
if (startTimeBtwSpawn > minTime)
{
startTimeBtwSpawn -= decreaseTime;
}
} else
{
timeBtwSpawn -= Time.deltaTime;
}
}
}
SpawnPoint
public class SpawnPoint : MonoBehaviour
{
//public GameObject enemy;
public ObjectPooler objectPooler;
public Vector2 enposition;
// Start is called before the first frame update
void Start()
{
objectPooler = ObjectPooler.Instance;
}
// Update is called once per frame
void Update()
{
objectPooler.SpawnFromPool("Cube", transform.position, Quaternion.identity).SetActive(true);
transform.position = new Vector2(transform.position.x, transform.position.y);
}
}
TheCubeItself
public class EnemyMovement : MonoBehaviour
{
public float speed;
public int damage = 1;
// Update is called once per frame
void FixedUpdate()
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
other.GetComponent<PlayerMovement>().health -= damage;
Debug.Log(other.GetComponent<PlayerMovement>().health);
Destroy(gameObject);
}
}
}
Any help or direction would be greatly appreciated.