C# 按下播放按钮时,统一冻结

C# 按下播放按钮时,统一冻结,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,我正在通过我的课程学习统一 现在我被困在一个点上,当我按下播放按钮时,unity屏幕冻结。 它发生在随机放置的敌人和加电的繁殖管理器中 敌方的IEnumerator工作得很好,但只要我在通电时添加相同的工具,并按下播放按钮,它就会冻结 当我在spawn管理器中注释掉powerup部分并重新启动unity时,它工作得非常好 我该怎么做才能让我的通电产卵正常工作 using System.Collections; using System.Collections.Generic; using Uni

我正在通过我的课程学习统一

现在我被困在一个点上,当我按下播放按钮时,unity屏幕冻结。 它发生在随机放置的敌人和加电的繁殖管理器中 敌方的
IEnumerator
工作得很好,但只要我在通电时添加相同的工具,并按下播放按钮,它就会冻结

当我在spawn管理器中注释掉powerup部分并重新启动unity时,它工作得非常好

我该怎么做才能让我的通电产卵正常工作

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);
        }
    }
}

在您的
PowerupSpawnRoutine
中,
while(true)
总是作为一个无限循环运行。你不会在它里面的任何地方
产生

您还应该在
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); 
    }
}

如中所述,您在
PowerupSpawnRoutine
中没有
yield
while(true)
循环中的任何位置


但是,作为这里的完整替代方案,您也可以在您的用例中使用,而不是使用协同程序:

[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);
}

谢谢你的回复。。。很抱歉我看到它太晚了…我后来自己解决了…无论如何谢谢你