简体   繁体   English

条件检查后,可以在Unity中协同运行协程

[英]The Coroutine in Unity functionality anamoly after condition checking

I'm new to Unity and I was just creating a simple galaxy shooter game where I want the enemies to be spawned only when the the player comes to existence. 我是Unity的新手,我只是在创建一个简单的银河射击游戏,我希望仅当玩家存在时才产生敌人。 So I have created a coroutine that checks for the playerExists condition, if it turns out to be true , it should further proceed in spawning the enemies every 5 seconds. 因此,我创建了一个协程以检查playerExists条件,如果结果为true ,则应每5秒进一步产生一个敌人。 But for some reason, it spawns just one enemy. 但是由于某种原因,它只会产生一个敌人。 Am I missing anything here ? 我在这里想念什么吗? Below is my SpawnManager where the spawning behaviour is controlled. 下面是我的SpawnManager,其中控制了生成行为。

public class SpawnManager : MonoBehaviour {

[SerializeField]
private GameObject _enemyShipPrefab;
[SerializeField]
private GameObject[] _powerUp;
UIManager _uimanager;


// Use this for initialization
void Start () {
    _uimanager = GameObject.Find("Canvas").GetComponent<UIManager>();
    StartCoroutine(SpawnPowerUps());
    StartCoroutine(SpawnEnemy());

}

IEnumerator SpawnEnemy(){
    while (_uimanager.playerExists == true)
    {
            Vector3 position = new Vector3(Random.Range(-8.23f, 8.23f), 5.7f, 0.0f);
            Instantiate(_enemyShipPrefab, position, Quaternion.identity);
            yield return new WaitForSeconds(5.0f);
    }

}

}

Below is my UIManager where i control the existence of the player. 下面是我的UIManager,用于控制播放器的存在。

public class UIManager : MonoBehaviour {

// Use this for initialization
public bool playerExists = false; 
public int playerScores = 0; 
public Sprite[] lives;
public Image playerLivesImagesToBeShown;
public Text playerScoreToBeShown;
public Image titleImage;
public GameObject playerPrefab;
void Start()
{

}

void Update()
{

        if (Input.GetKeyDown(KeyCode.Space) && playerExists == false){
            titleImage.gameObject.SetActive(false);
            Instantiate(playerPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            playerScores = 0;
            playerScoreToBeShown.text = "Score : 0";
            playerExists = true;
        }


}


public void updateLives(int livesToView ){
    playerLivesImagesToBeShown.sprite = lives[livesToView];
            if(livesToView == 0){
                playerExists = false;
                titleImage.gameObject.SetActive(true);
            }
}

At the first glace of your code (and the described problem) i would say, your SpawnEnemy() Coroutine runs through and exit afterwards. SpawnEnemy() ,您的代码(以及所描述的问题) SpawnEnemy() ,您的SpawnEnemy()协程会一直运行并随后退出。 You have to lock it in some loop, like this: 您必须将其锁定在某个循环中,如下所示:

IEnumerator SpawnEnemy ()
{
    while (true) // Keep checking
    {
        if(_uimanager.playerExists == true)
        {
            Vector3 position = new Vector3(Random.Range(-8.23f, 8.23f), 5.7f, 0.0f);
            Instantiate(_enemyShipPrefab, position, Quaternion.identity);            
            yield return new WaitForSeconds (5.0f);  // After spawning waits 5secs
        }
        yield return null; // Starts loop with next frame.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM