【问题标题】:The hazards in my unity game are not spawning我的团结游戏中的危险没有产生
【发布时间】:2019-03-27 13:08:27
【问题描述】:

我有一个游戏,你是一个玩家方块,你必须在其他方块经过你时躲避它们。由于我调用了一个名为 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;
}

}

我希望敌人会生成,但他们没有

【问题讨论】:

  • hazardCount 的值是多少,您从哪里调用GameOver
  • 那么startWait、waveWait和spawWait的值是多少?
  • 当您添加调试消息时,您的代码会到达哪里?
  • 我认为应该是yield break 而不是break
  • @Dave,这不会有任何区别。 break 用于退出无限的 while 循环。协程将在循环退出后结束。

标签: c# unity3d


【解决方案1】:

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

所以在我看来,这与检查员的价值观有关。 检查以下内容: 您是否检查过您的 hazardCount 大于零? 您的 hazardArray 是否完全充满了预制件? (如果不是,那应该会导致不生成,但也应该导致空引用异常。)

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

Offtopic: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)。

【讨论】:

  • 非常感谢你的回答,我刚刚删除了分数文本,因为它对我没有用,游戏控制器又开始工作了。再次感谢您的回答。
  • 但现在我的问题是游戏结束和重启功能不起作用\
  • 任何异常或警告或什么都没发生?
  • 好的。因为事实上,我第一个可能的猜测是没有抛出异常,场景没有正确添加到构建中,不再可能。我建议您检查其他脚本并将 Debug.Log() 添加到您的 Gameover() 方法。但我认为由于另一个脚本中的问题,该方法根本没有被执行。如果您可以在新问题或此处的评论部分中提供有关其他脚本或检查器值的更多信息,那也不错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 2021-08-24
  • 1970-01-01
  • 2014-11-14
  • 2013-11-30
  • 1970-01-01
相关资源
最近更新 更多