How do i make everything stop after my player dies besides my background?

Hi ya’ll :grin:,

I have everything up and running so far, new to unity so please don’t judge lol

I use a PlayerSpawner and EnemySpawner and of course my background script see scripts below.

PlayerSpawner script:

public class PlayerSpawner : MonoBehaviour
{

    public GameObject playerPrefab;
    GameObject playerInstance;

    public int numLives = 4;

    float respawnTimer;

    // Use this for initialization
    void Start()
    {
        SpawnPlayer();
    }

    void SpawnPlayer()
    {
        numLives--;
        respawnTimer = 1;
        playerInstance = (GameObject)Instantiate(playerPrefab, transform.position, Quaternion.identity);
    }

    // Update is called once per frame
    void Update()
    {

        if (playerInstance == null && numLives > 0)
        {
            respawnTimer -= Time.deltaTime;

            if (respawnTimer <= 0)
            {
                SpawnPlayer();
            }
 
        }
    }   


    void OnGUI()
    {

        if (numLives > 0 || playerInstance != null)
        {
            GUI.Label(new Rect(0, 0, 100, 50), "Lives Left: " + numLives);
        }
        else
        {   
            GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), "Game Over");

        }
    }
}

EnemySpawner script:

public class EnemySpawner : MonoBehaviour
{

    public GameObject[] enemyPrefabs;

    float spawnDistance = 12f;

    float enemyRate = 5;
    float nextEnemy = 1;


    void Update()
    {
        nextEnemy -= Time.deltaTime;

        if (nextEnemy <= 0)
        {
            nextEnemy = enemyRate;
            enemyRate *= 0.9f;
            if (enemyRate < 2)
                enemyRate = 2;

            Vector3 offset = Random.onUnitSphere;

            offset.z = 0;

            offset = offset.normalized * spawnDistance;

            // the method UnityEngine.Random.Range will give you an int between 0 and (prefabs count - 1)
            // so the value can be used as an index to pick the next enemy
            int randomIndex = UnityEngine.Random.Range(0, enemyPrefabs.Length);
            GameObject nextPrefab = enemyPrefabs[randomIndex];
            Instantiate(nextPrefab, transform.position + offset, Quaternion.identity);
        }
    }

Background script:

public class ScrollUV : MonoBehaviour
{

    void Update()
    {

        MeshRenderer mr = GetComponent<MeshRenderer>();

        Material mat = mr.material;

        Vector2 offset = mat.mainTextureOffset;

        offset.y += Time.deltaTime / 10f;

        mat.mainTextureOffset = offset;

    }

}

So when the character dies the enemies still keep on respawning, I would like that to stop once the player dies.

Thanks in advance for your help.

Why don’t you hook those spawners to some controller object that owns the player? That way, you could tell if the player is alive or dead, and simply stop spawning enemies (and even restart once new player is spawned).

You could also just set timeScale to 0 when there is no player, but that will stop everything that’s not using unscaled time.