我的统一游戏中的危害没有产生

时间:2019-03-27 13:08:27

标签: c# unity3d

我有一个游戏,您在其中是一个玩家多维数据集,您必须躲避其他多维数据集,因为它们会通过您的方式。敌人的多维数据集应该由于我称为GameController的脚本而产生。但是多维数据集没有生成。请帮忙。 (此外,每当我的多维数据集被破坏时,游戏结束和重新启动功能均无法正常工作)

我尝试重新创建预制件和代码,但是没有任何反应。另外,相同的代码也可以在我的其他游戏中使用 这是我的代码:

void Start()
{
    gameOver = false;
    restart = false;
    restartText.text = "";
    gameOverText.text = "";
    score = 0;
    UpdateScore();
    StartCoroutine(SpawnWaves());
}

void Update()
{
    if (restart)
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

        }
    }
}

IEnumerator SpawnWaves()
{
    yield return new WaitForSeconds(startWait);
    while (true)
    {
        for (int i = 0; i < hazardCount; i++)
        {
            GameObject hazard = hazards[Random.Range(0, hazards.Length)];
            Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
            Quaternion spawnRotation = Quaternion.identity;
            GameObject newSpawn = Instantiate(hazard, spawnPosition, spawnRotation) as GameObject;
            yield return new WaitForSeconds(spawnWait);
        }
        yield return new WaitForSeconds(waveWait);

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

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

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

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

}

我希望敌人产卵,但他们不是

1 个答案:

答案 0 :(得分:1)

我已将您的示例代码复制并粘贴到示例项目中。在此示例中,您的脚本按预期工作,正如您已经提到的,它可以在其他项目中工作。

因此,我认为这与检查员的价值观有关。 检查以下内容: 您是否检查过hazardCount大于零? 您的hazardArray是否已完全填充预制件? (否则,将导致未生成,但也会导致null-reference-exception。)

另一种选择是您以某种方式制作屏幕截图或在检查器中提供值,以便重现错误。

关闭主题:gameOver之后的重新启动被延迟,因为if(gameOver)-Clause出现在WaitForSeconds(waveWait)之后。我的建议是将其更改为以下内容:

public GameObject[] hazards;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public Text scoreText;
public Text restartText;
public Text gameOverText;
private bool gameOver;
private bool restart;
private int score;

private Coroutine gameRoutine;


void Start()
{
    gameOver = false;
    restart = false;
    restartText.text = "";
    gameOverText.text = "";
    score = 0;
    UpdateScore();
    gameRoutine = StartCoroutine(SpawnWaves());
}

void Update()
{
    if (restart)
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

        }
    }
}

IEnumerator SpawnWaves()
{
    yield return new WaitForSeconds(startWait);
    while (true)
    {
        for (int i = 0; i < hazardCount; i++)
        {
            GameObject hazard = hazards[Random.Range(0, hazards.Length)];
            Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
            Quaternion spawnRotation = Quaternion.identity;
            GameObject newSpawn = Instantiate(hazard, spawnPosition, spawnRotation) as GameObject;
            yield return new WaitForSeconds(spawnWait);
        }
        yield return new WaitForSeconds(waveWait);    
    }
}

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

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

public void GameOver()
{
    gameOverText.text = "Game Over!";
    restartText.text = "Press 'R' for Restart";
    restart = true;
    gameOver = true;
    StopCoroutine(gameRoutine);
}

我在这里所做的更改是,删除了if条件,并且协程现在存储在Start()中,并在执行gameOver()方法时直接停止。代替StopCoroutine()的另一种方法是将while循环的条件设置为while(!gameOver)。