【发布时间】:2018-05-16 06:42:40
【问题描述】:
我是 Unity 新手,我只是在创建一个简单的银河射击游戏,我希望只有在玩家出现时才会生成敌人。所以我创建了一个协程来检查playerExists 条件,如果结果是true,它应该进一步每5 秒产生一次敌人。但由于某种原因,它只产生一个敌人。我在这里错过什么了吗?
下面是我的 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);
}
}
}
下面是我的 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);
}
}
【问题讨论】: