Problem with decrement
Hello ! This is probably the noobest question ever, but I'm really new to programming, and I'm struggling on a pretty basic step. I'm making a little "spaceship shooting humans" game, and I made a spawner with a Coroutine, spawning multiples enemies at random times. I also made an "enemies limit", with a variable detecting how many enemies are spawned, and how many enemies are allowed:
public GameObject[] enemies;
public Vector3 spawnValues;
public float spawnWait;
public float spawnMostWait;
public float spawnLeastWait;
public int startWait;
public bool stop;
public int enemiesSpawned;
public int maxEnemies;
int randEnemy;
(I followed a tutorial for the spawner)
void Start()
{
StartCoroutine(waitSpawner());
}
void Update()
{
spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
if (maxEnemies <= enemiesSpawned)
{
stop = true;
}
else
{
stop = false;
}
}
And the Coroutine:
IEnumerator waitSpawner()
{
yield return new WaitForSeconds(startWait);
while (!stop)
{
randEnemy = Random.Range(0, 2);
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1);
Instantiate(enemies[randEnemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
enemiesSpawned++;
yield return new WaitForSeconds(spawnWait);
}
}
I hope you can follow with my really messy script. Now here is the thing: In a the prefab of my enemy, I made a reference to a function in my spawner script using "public Spawner Spawner;"; and "Spawner.EnemyDecrease();"
Running the function in the Spawner Script works well, since I can print a Debug.Log when an enemy dies, but when I'm trying to decrement "enemiesSpawned", so I can make enemies spawn even when some are dead, it just doesn't work. I just feel so dumb for struggling on such a basic step, but please, help me making this decrement right. Thank you !
public void EnemyDecrease()
{
enemiesSpawned--;
}
Your answer
Follow this Question
Related Questions
C# decreasing float slightly incorrect (0.4999999) 1 Answer
I need to activate a function in another script from a script on a different game object 0 Answers
How to give Vector3 position to object being instantiated 1 Answer
How to get a for loop to run once per coroutine? 1 Answer
yield return StartCoroutine() doesnt wait for coroutine to finish 5 Answers