[Unity在程序最后一次通过WaitForSeconds时崩溃

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

我有一个奇怪的问题。我的代码工作正常。一切都会按预期进行,直到最后一次通过foreach循环为止。代码:

public List<string> waveInput = new List<string>(); //die Eingabe der Welle als Zeichenfolge
public List<GameObject> enemyTyps = new List<GameObject>();
public List<Vector3> path = new List<Vector3>();
public GameObject tempPathStart;
public int currentWave = 0;
public int currentAmountOfEnemies;

private int spawnAmountOfEnemies;
private double spawnDelay;
private List<GameObject> enemyToSpawn = new List<GameObject>();

void Start() {
    path.Add(tempPathStart.transform.position);
    StartCoroutine(function());
}

IEnumerator function() {

    while(waveInput.Capacity >= currentWave) {

        if(waveInput[currentWave] == "" && currentAmountOfEnemies <= 0) {
            currentWave++;
            enemyToSpawn.Clear();
            spawnAmountOfEnemies = 0;
        } else if(currentAmountOfEnemies <= 0) {
            string _substring = waveInput[currentWave].Substring(0, waveInput[currentWave].IndexOf(";") + 1);
            ManageSubstring(_substring);

            for(int i = 0; i < spawnAmountOfEnemies; i++) {

                foreach(GameObject element in enemyToSpawn) {
                    this.SpawnEnemy(element);
                    yield return new WaitForSeconds((float)spawnDelay);
                }
            }
        }
    }
}

void ManageSubstring(string _substring) {
    string _tempStringAmount = "";
    string _tempStringDelay = "";
    string _tempStringType = "";
    bool _switchAmountDelay = false;

    for(int i = 0; i < _substring.Length; i++) {
        char c = _substring[i];

        if(c >= '0' && c <= '9') {

            if(_switchAmountDelay) {

                _tempStringDelay += c;
            } else {
                _tempStringAmount += c;
            }
        } else if(c == ';') {

        } else if(c == '.') {
            _tempStringDelay += c;
        } else {
            _switchAmountDelay = true;
            _tempStringType += c;
        }
    }
    spawnDelay = double.Parse(_tempStringDelay);
    spawnAmountOfEnemies = int.Parse(_tempStringAmount);

    foreach(char c in _tempStringType) { //die Buchstaben in GameObjekte / Gegner umwandeln
        int _tempConvertedInt = TranslateStringToInt(c);
        enemyToSpawn.Add(enemyTyps[_tempConvertedInt]);
    }
}

int TranslateStringToInt(char pToConvertChar) {
    List<char> _alphabet = new List<char>() {'a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    return _alphabet.IndexOf(pToConvertChar);
}

void SpawnEnemy(GameObject prefab) {
    currentAmountOfEnemies++;
    GameObject e = Instantiate(prefab) as GameObject;
    e.transform.position = path[0];
}

我已经说过的非常奇怪的事情是:代码有效,即使该行在最后一次运行之前也可以正常工作。然后Unity崩溃,我不知道该怎么办。如果上下文需要更多代码,请说。

谢谢您的回答!

c# unity3d crash ienumerator
1个回答
0
投票

我刚刚将您的脚本拖入调试器,并完成了最后一个迭代:

© www.soinside.com 2019 - 2024. All rights reserved.