I can’t for the life of me figure out why this method is messing up, it wasn’t before, I’ve been combing through my scripts and can’t for the life of me figure out what’s going on here. I have a big eats small type of game every time you or an enemy eats an enemy, it calls the SpawnEnemy() function and destroys the enemy(I understand I need to make a pool, I’m just working that out still lol)
But for some reason, it seems to be calling the SpawnEnemy function twice? I tested it with a very small amount of enemies, and it seems to be the case. But I can’t figure out why. It only instantiates once, and it’s an OnTriggerEnter() that calls the SpawnEnemy() method. Here is my Spawn Manager script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
private static EnemySpawner _spawner;
public GameObject enemy;
private Mesh mesh;
float planeX, planeZ;
public int enemiesToSpawn, enemiesSpawned;
private bool init;
private BadBall lastEnemy;
Player player;
public List<GameObject> balls;
private Collider[] hitCollider;
float lastEnemySize = 5;
LayerMask ground;
public static EnemySpawner Spawner
{
get
{
if (_spawner == null)
{
print("Spawner is null?");
}
return _spawner;
}
}
void Awake()
{
ground = LayerMask.GetMask("Ground");
player = FindObjectOfType<Player>();
_spawner = this;
}
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
planeX = mesh.bounds.size.x;
planeZ = mesh.bounds.size.z;
for (int i = 0; i < enemiesToSpawn; i++)
{
SpawnEnemy();
}
init = true;
}
public void SpawnEnemy()
{
float randX = Random.Range(-planeX* transform.localScale.x/2, planeX*transform.localScale.x/2);
float randY = Random.Range(-planeZ*transform.localScale.z / 2, planeZ*transform.localScale.z / 2);
Vector3 spawnPoint = new Vector3(randX, 1, randY);
if (!init)
{
hitCollider = Physics.OverlapSphere(spawnPoint, 10, ~ground);
}
else
{
hitCollider = Physics.OverlapSphere(spawnPoint, lastEnemySize * 5,~ground);
}
if (hitCollider.Length < 1)
{
//if (balls.Count < enemiesToSpawn)
balls.Add(Instantiate(enemy, spawnPoint, Quaternion.identity));
lastEnemySize = balls[balls.Count - 1].GetComponent<BadBall>().maxSize;
enemiesSpawned++;
}
else
{
SpawnEnemy();
}
}
}
It calls the SpawnEnemy() a second time at the end there so as to not make enemies spawn on top of each other, but it doesn’t instantiate in that case. Here is the one method that calls the SpawnEnemy() method,
private void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.CompareTag("Player"))
{
if (collision.gameObject.GetComponent<Player>().size < size)
{
player.isDead = true;
StartCoroutine(player.RestartGame());
sizeGoal += collision.gameObject.GetComponent<Player>().size / 10;
Destroy(collision.GetComponent<SphereCollider>());
}
}
if (collision.gameObject.CompareTag("Enemy"))
{
if (size>collision.gameObject.GetComponent<BadBall>().size)
{
Destroy(collision.GetComponent<SphereCollider>());
EnemySpawner.Spawner.balls.Remove(collision.gameObject);
Destroy(collision.gameObject);
sizeGoal += collision.GetComponent<BadBall>().size / 10;
EnemySpawner.Spawner.SpawnEnemy();
}
}
}
I added destroy sphere collider just in case it was colliding twice in a split second, but no luck. Am I blind? I feel like it should be so obvious, but I can’t figure it out. It spawns the correct number of enemies in Start() but the number skyrockets after a minute or two and the game becomes unplayable lol.
Thanks in advance
Edit: Also I made the “If(balls.Count< enemiesToSpawn)” as a temporary fix, but obviously that’s a band-aid solution lol