Spawn enemy while player is alive....

I am trying to spawn enemies while the player is still alive, however nothing is being spawned at all. Any Ideas why? I am new to programming so if it is a simple fix please forgive me :slight_smile:

Here is my code

public class CallSpawn : MonoBehaviour
{
    public Transform astroid;
    public Vector3 position;
    public int count = 0;

    public void Spawn()
    {
        while (GameMaster.isDead == 0)
        {
            count = count + 1;
            if (0 == (count % 10))
            {
                position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
                Instantiate(astroid, position, Quaternion.identity);
            }

        }
    }
}

Class being called

using UnityEngine;
using System.Collections;

public class GameMaster : MonoBehaviour {
    public static int isDead = 0;
	public static void KillPlayer(Player player)
    {
        Destroy(player.gameObject);
        isDead = 1;
    }
    public static void KillEnemy(Enemy astroid)
    {
        Destroy(astroid.gameObject);
    }
    
}

The biggest problem I see is that your spawn function is an infinite loop, after entering the while GameMaster.isDead can never change from 0. Also my guess is that you are trying to use your count as a delay timer between spawns, if that is the case you can put something like this in the CallSpawn update and never have to worry about manually calling spawn.

private float spawnDelay = 10;
private float timeUntilSpawn = 0;

void Update(){
  if(GameMaster.isDead == 0){
    if(timeUntilSpawn <=0{
           position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
           Instantiate(astroid, position, Quaternion.identity);
           timeUntilSpawn = spawnDelay
    }else{
        timeUntilSpawn -= Time.deltaTime;
    }
  }
}

One other note to help readability would be to change isDead from an int to a bool if you only have dead or alive.

public static bool isDead = false;
public static void KillPlayer(Player player){
    isDead = true;
}

And then you don’t have to worry about comparing to 1 or 0 in your checks with it, simply if(GameMaster.isDead) or if(!GameMaster.isDead)

You’ll want something more like this: (Untested)

public class CallSpawn : MonoBehaviour
{
	public GameObject astroid; //this should be a GameObject rather than a transform. 
	public float spawnRate = 10f; //enemy spawn rate in seconds

	private bool shouldSpawn = false;
	private bool nextSpawnTime;
	
	public void StartSpawning()
	{
		nextSpawnTime = Time.time + spawnRate;
		shouldSpawn = true;
	}

	void Update() //update is called once each frame
	{
		if(shouldSpawn == false) return; //do not run the below code if the enemies shouldnt spawn

		if(GameMaster.isDead == true) //replace GameMaster.isDead with a bool. It's much cleaner.
		{
			shouldSpawn = false;
		}

		if(Time.time > nextSpawnTime)
		{
			Vector3 position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
			Instantiate(astroid, position, Quaternion.identity);
			nextSpawnTime += spawnRate;
		}

	}
}