【问题标题】:Unity crashes as the program goes through WaitForSeconds the last time当程序最后一次通过 WaitForSeconds 时,Unity 崩溃
【发布时间】:2020-08-05 23:30:17
【问题描述】:

我有一个奇怪的问题。我的代码工作正常。一切正常,直到最后一次通过 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 崩溃了,我不知道该怎么办。如果上下文需要更多代码,请说。

感谢您的每一个回答!

【问题讨论】:

  • 那里有很多未知的变量内容,waveInput.capacity 不是它的长度,所以它可能会在很长一段时间内继续循环的第一个位或崩溃,因为 waveinput 是没那么长..
  • 您是否有机会添加有关崩溃的堆栈跟踪或其他输出?
  • 统一只是冻结,这意味着没有输出,这就是为什么我问堆栈的用户:D

标签: c# unity3d crash ienumerator


【解决方案1】:

我刚刚将您的脚本拉入调试器并逐步完成了最后一次迭代:

当迭代结束时,它会返回到 while() 循环,该循环包含了function() 的整个主体。

  • while 条件保持为真,因为 waveInput.Capacity 大于 currentWave(仍然为零)。
  • if(waveInput[currentWave] == "" &amp;&amp; currentAmountOfEnemies &lt;= 0) 为 false,因为 waveInput[] 数组的零元素不是“”,并且敌人计数尚未重置。
  • else if(currentAmountOfEnemies &lt;= 0) 条件也为假,因为当前金额未重置。
  • while 循环中没有更多内容,没有任何变化,您有一个无限循环,它永远不会达到产量回报。这会锁定 Unity 的 Update() 循环,并且只允许杀死。

【讨论】:

  • 注意:我只是将它添加到一个空对象中,将两个预制件链接为 spawns,并设置两个波输入,将此字符串分配给 test:“6a1.25;6a1.25;”跨度>
  • 感谢您的回答。有几件事我忘了提。就像如果一个对象被破坏, currentAmountOfEnemies 会减一。我做了一些改变,现在团结不再冻结。仍然不完美,但我今天取得了进步,你帮了很多忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 2012-09-15
  • 2015-03-02
  • 2014-11-06
  • 1970-01-01
相关资源
最近更新 更多