IEnumerator skips the code after yield return new WaitForSeconds

116 views Asked by At

I am trying to use coroutines to spawn cubes in my game by following Unity Learn tutorials but altougj I did everything same as it is in the tutorial the code does not work and skips the code after yield return new WaitForSeconds. I already looked to questions in stackoverflow and only I could find is someone with the same problem but one and only accepted answer is " "probably" killing the scene before the WaitForSeconds-inputed time pass" and the code is true but there is something wrong in the scene. So, as a newbie Unity and C# user I could not solve it.

I used this code for difficulty buttons in my game;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DifficultyButton : MonoBehaviour
{
    private GameManager gameManager;
    private Button button;
    // Start is called before the first frame update
    void Start()
    {
        button = GetComponent<Button>();
        button.onClick.AddListener(SetDifficulty);
        gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
    }

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

    void SetDifficulty()
    {
        Debug.Log(gameObject.name + " was clicked");
        gameManager.StartGame();
    }
}

And the GameManager script used in this button script is this;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Runtime.CompilerServices;

public class GameManager : MonoBehaviour
{
    public List<GameObject> targets = new List<GameObject>(4);
    public TextMeshProUGUI scoreText;
    public TextMeshProUGUI gameOverText;
    public Button restartButton;
    public bool isGameActive;

    private float score = 0;
    private float spawnRate = 1.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    IEnumerator SpawnTarget() 
    {
        Debug.Log("hop ordayım");
        Debug.Log("isGameActive: " + isGameActive);
        while (isGameActive)
        {
            Debug.Log("Sonunda burdayım");
            
            yield return new WaitForSeconds(spawnRate);
            Debug.Log("Buraya da geldim");
            int index = Random.Range(0 ,targets.Count);
            Debug.Log("Buraya da");
            Instantiate(targets[index]);
            Debug.Log("cok sukur be");
        }

    }

    public void UpdateScore(int scoreToAdd) 
    {
        score += scoreToAdd;
        scoreText.text = "Score:" + score;
    }

    public void GameOver() 
    {
        gameOverText.gameObject.SetActive(true);
        restartButton.gameObject.SetActive(true);
        isGameActive = false;
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void StartGame()
    {
        isGameActive = true;
        StartCoroutine(SpawnTarget());
        Debug.Log("geldim");
        UpdateScore(0);
    }
}


I used Debug.Log's to see what is happening and the output when I clicked the easy button is this;

Easy Button was clicked
hop ordayım
isGameActive: True
Sonunda burdayım
geldim

So it does not continue after printing "geldim" altough I wait more than 1 seconds after "geldim" printed;

Expected output;

...
geldim
Buraya da geldim
Buraya da
cok sukur be

Why is this happening and what can I do about this?

1

There are 1 answers

1
Dylan Santwani On

add yield return null to the end