当按下播放键时,Unity冻结。

问题描述 投票:0回答:1

我正在通过一个udemy课程学习Unity。

现在,我卡在一个点,unity屏幕冻结,当我点击播放按钮.它发生在随机定位的敌人和powerupsthe的产卵管理器。IEnumerator 敌人的部分可以完美地工作,但只要我为电源添加同样的内容,并点击播放按钮,它就会冻结。

当我在产卵管理器中注释出powerup部分,并重新启动unity时,它完美地工作。

我应该怎么做才能让我的powerup spawn工作?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    [SerializeField]
    private GameObject _enemyShipPrefab;
    [SerializeField]
    private GameObject[] powerup;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(EnemySpawnRoutine());
        StartCoroutine(PowerupSpawnRoutine());
    }

    public IEnumerator EnemySpawnRoutine()
    {
        while(true)
        {
            float enemyPositionX = Random.Range(-8.34f, 8.34f);
            Instantiate(_enemyShipPrefab, new Vector3(enemyPositionX, 6.39f, 0), Quaternion.identity);
            yield return new WaitForSeconds(5.0f);
        }
    }

    public IEnumerator PowerupSpawnRoutine()
    {
        while(true)
        {
            // float powerupPositionX = Random.Range(-8.622f, 8.622f);
            int randomPowerup = Random.Range(0, 3);
            Instantiate(powerup[randomPowerup], new Vector3(Random.Range(-8.622f, 8.622f), 4.5f, 0), Quaternion.identity);
        }
    }
}
c# visual-studio unity3d
1个回答
2
投票

在你的 PowerupSpawnRoutinewhile(true) 总是要运行,因为它是一个无限循环。你不需要 yield 内的任何地方。

你应该在你的 PowerupSpawnRoutine 也是。

public IEnumerator PowerupSpawnRoutine()
{
    while(true)
    {
        // float powerupPositionX = Random.Range(-8.622f, 8.622f);
        int randomPowerup = Random.Range(0, 3);
        Instantiate(powerup[randomPowerup], new Vector3(Random.Range(-8.622f, 8.622f), 4.5f, 0), Quaternion.identity);

        // this e.g. will spawn powerup after every 5 seconds.
        yield return new WaitForSeconds(5.0f); 
    }
}

0
投票

如前所述。Muhammad Farhan Aqeel 你不是 yield内的任何地方。while(true) 循环 PowerupSpawnRoutine.


然而,作为一个完整的替代方案,在这里你可以不使用Coroutines,而是使用 InvokeRepeating 在您的用例中。

[SerializeField] private float enemySpawnDelay = 5f;
[SerializeField] private float powerupSpawnDelay = 5f;

// Start is called before the first frame update
void Start()
{
    // The first parameter is the initial delay so 0 makes the first call
    // immediately.
    // The second parameter is the repeating delay
    InvokeRepeating(nameof(EnemySpawnRoutine), 0f, enemySpawnDelay);
    InvokeRepeating(nameof(PowerupSpawnRoutine), 0f, powerupSpawnDelay);
}

private void EnemySpawnRoutine()
{
    float enemyPositionX = Random.Range(-8.34f, 8.34f);
    Instantiate(_enemyShipPrefab, new Vector3(enemyPositionX, 6.39f, 0), Quaternion.identity);
}

private void PowerupSpawnRoutine()
{
    // when getting random indices use the actual length of the array to be more
    // flexible and secure against errors
    int randomPowerup = Random.Range(0, powerup.Length);
    Instantiate(powerup[randomPowerup], new Vector3(Random.Range(-8.622f, 8.622f), 4.5f, 0), Quaternion.identity);
}
© www.soinside.com 2019 - 2024. All rights reserved.