Extending Space Shooter - Stopping Coroutines, as well as updating, stopping, and calling methods

hello…

Apologies for the beginner question, but I’m stumped.

In extending the space shooter project I would like to introduce new coroutines I have added to the game. For example, when the Player reaches a certain score, one or more running coroutines will stop and one or more will start.

Additional when the Player Character has an OnTriggerEnter() collision with a particular Object with the ‘LevelSwtich’ script, one or several coroutines will stop and another will begin.

What I’d like to do is continually update a method called UpdateLevel() which resides in start(). Meaning I’ve placed the IENumerator coroutines within other methods, such as MainLevel(), that will be called or stopped within UpdateLevel() given certain conditions.

How do I do this?

THANKS!!! :slight_smile:

public class GameController : MonoBehaviour
{
    /// <summary>
    /// Coroutine Declarations!!!!!!!!!
    /// </summary>

    public GameObject[] Asteroids;
    public Vector3 asteroidSpawnValues;
    public int asteroidCount;
    public float asteroidSpawnWait;
    public float asteroidStartWait;
    public float asteroidWaveWait;

    public IEnumerator asteroidsMain()
    {
        yield return new WaitForSeconds(asteroidStartWait);
        while (true)
        {
            for (int i = 0; i < asteroidCount; i++)
            {
                GameObject asteroid = Asteroids[Random.Range(0, Asteroids.Length)];
                Vector3 spawnPosition = new Vector3(Random.Range(-asteroidSpawnValues.x, asteroidSpawnValues.x), asteroidSpawnValues.y, asteroidSpawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate(asteroid, spawnPosition, spawnRotation);
                yield return new WaitForSeconds(asteroidSpawnWait);
            }

            yield return new WaitForSeconds(asteroidWaveWait);

            if (gameOver)
            {
                restartText.text = "Press 'R' for Restart";
                restart = true;
                break;
            }

        }
    }

    public GameObject[] Crystals;
    public Vector3 crystalSpawnValues;
    public int crystalCount;
    public float crystalSpawnWait;
    public float crystalStartWait;
    public float crystalWaveWait;

    public IEnumerator crystalsMain()
    {

        yield return new WaitForSeconds(crystalStartWait);
        while (true)
        {
            for (int i = 0; i < crystalCount; i++)
            {
                GameObject crystal = Crystals[Random.Range(0, Crystals.Length)];
                Vector3 spawnPosition = new Vector3(Random.Range(-crystalSpawnValues.x, crystalSpawnValues.x), crystalSpawnValues.y, crystalSpawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate(crystal, spawnPosition, spawnRotation);
                yield return new WaitForSeconds(crystalSpawnWait);
            }

            yield return new WaitForSeconds(crystalWaveWait);

            if (gameOver)
            {
                restartText.text = "Press 'R' for Restart";
                restart = true;
                break;
            }
        }

    }

    public GameObject[] PowerUps;
    public Vector3 powerUpSpawnValues;
    public int powerUpCount;
    public float powerUpSpawnWait;
    public float powerUpStartWait;
    public float powerUPWaveWait;

    public IEnumerator PowerUpMain()
    {

        yield return new WaitForSeconds(powerUpStartWait);
        while (true)
        {
            for (int i = 0; i < powerUpCount; i++)
            {
                GameObject powerUp = PowerUps[Random.Range(0, PowerUps.Length)];
                Vector3 spawnPosition = new Vector3(Random.Range(-powerUpSpawnValues.x, powerUpSpawnValues.x), powerUpSpawnValues.y, powerUpSpawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate(powerUp, spawnPosition, spawnRotation);
                yield return new WaitForSeconds(powerUpSpawnWait);
            }

            yield return new WaitForSeconds(crystalWaveWait);

            if (gameOver)
            {
                restartText.text = "Press 'R' for Restart";
                restart = true;
                break;
            }
        }

    }

    public GameObject[] AsteroidTunnels;
    public Vector3 asteroidTunnelSpawnValues;
    public int asteroidTunnelCount;
    public float asteroidTunnelSpawnWait;
    public float asteroidTunnelStartWait;
    public float asteroidTunnelWaveWait;

    public IEnumerator asteroidTunnelsMain()
    {
        yield return new WaitForSeconds(asteroidTunnelStartWait);
        while (true)
        {
            for (int i = 0; i < asteroidCount; i++)
            {
                GameObject asteroidTunnel = AsteroidTunnels[Random.Range(0, AsteroidTunnels.Length)];
                Vector3 spawnPosition = new Vector3(Random.Range(-asteroidTunnelSpawnValues.x, asteroidTunnelSpawnValues.x), asteroidTunnelSpawnValues.y, asteroidTunnelSpawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate(asteroidTunnel, spawnPosition, spawnRotation);
                yield return new WaitForSeconds(asteroidTunnelSpawnWait);
            }

            yield return new WaitForSeconds(asteroidTunnelWaveWait);

            if (gameOver)
            {
                restartText.text = "Press 'R' for Restart";
                restart = true;
                break;
            }

        }
    }

    /*public GameObject[] AsteroidLevel1;
    public Vector3 asteroidlvl1SpawnValues;
    public int asteroidLvl1Count;
    public float asteroidLvl1SpawnWait;
    public float asteroidLvl1StartWait;
    public float asteroidLvl1WaveWait;*/


    /// <summary>
    /// Game Variable Declarations
    /// </summary>

    public GUIText scoreText;
    public GUIText cashText;
    public GUIText restartText;
    public GUIText gameOverText;

    private bool gameOver;
    private bool restart;
 
    private int score;
    private int cash;

 
    public Coroutine coAsteroids;
    public Coroutine coCrystals;
    public Coroutine coPowerUps;
    public Coroutine coAsteroidTunnel;

 




    /// <summary>
    /// START START START START START START
    /// </summary>

    void Start()
    {
        gameOver = false;
        restart = false;
        restartText.text = "";
        gameOverText.text = "";
        cashText.text = "";
        score = 0;
        cash = 0;
        UpdateScore();
        UpdateCash();
        UpdateLevel();
     
    }

    void Update()
    {
        if (restart)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                Application.LoadLevel(Application.loadedLevel);
            }
        }
    }
 
   public void UpdateLevel()
    {
        MainLevel();
        // Start AsteroidTunnel()
        if (score>= 500)
        {
            AsteroidTunnel();
        }
     
    }

    public void MainLevel() {
        coAsteroids = StartCoroutine(asteroidsMain());
        coCrystals = StartCoroutine(crystalsMain());
        coPowerUps = StartCoroutine(PowerUpMain());
    }

    public void AsteroidTunnel()
    {
     
        //stop MainLevel()
        StopCoroutine(coAsteroids);
        StopCoroutine(coCrystals);
        StopCoroutine(coPowerUps);
        //Start AsteroidTunnel()
        coAsteroidTunnel = StartCoroutine(asteroidTunnelsMain());
    }

    /// <summary>
    /// SCORE SCORE SCORE SCORE SCORE SCORE/////////////////////////////////////////////
    /// </summary>


    public void AddScore(int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }

    void UpdateScore()
    {
        scoreText.text = "" + score;
    }

    //
    // CASH CASH CASH CASH CASH CASH CASH CASH //////////////////////////////////////////
    //

    public void AddCash(int newCashValue)
    {
        cash += newCashValue;
        UpdateCash();
    }

    void UpdateCash()
    {
        cashText.text = "" + cash;
    }

    /// <summary>
    /// GAME OVER
    /// </summary>

    public void GameOver()
    {
        gameOverText.text = "Game Over";
        gameOver = true;
    }
}

… and levelSwitch.

public class levelSwitch : MonoBehaviour
{

    private GameController gameController;
    public bool lvlSwitch1 = false;

    void Start()
    {
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        if (gameController == null)
        {
            Debug.Log("Cannot find 'GameController' script");
        }
    }

    void OnTriggerEnter(Collider other)
    {

        if (other.tag == "Player")
        {
            Destroy(gameObject);
            //gameController.????????;   stop current Corountine and start a new one.


        }
    }
}

Any Thoughts?