首页 > 解决方案 > 在 for 循环中使用 Instantiate 时 Unity 崩溃

问题描述

using UnityEngine;

public class SpawnIll : MonoBehaviour
{
    public int numberIll;
    public GameObject illPrefab;
    public Vector2 pos;
    void Start()
    {
        for (int i = 1; i <= numberIll; i++)
        {
          pos=new Vector2(Random.Range(-1000,1001),Random.Range(-1000,1001));
            Instantiate(illPrefab, pos, Quaternion.identity);
        }
    }

}

无论我选择什么作为 numberIll,Unity 都会崩溃。我认为它进入了一个无限循环,但我不知道我做错了什么。

标签: visual-studiounity3dcrash

解决方案


已解决:创建的每个对象都在执行 for 循环,因此它只会创建大量游戏对象。我使用了协程,它奏效了。


推荐阅读