visual-studio - 在 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 都会崩溃。我认为它进入了一个无限循环,但我不知道我做错了什么。
解决方案
已解决:创建的每个对象都在执行 for 循环,因此它只会创建大量游戏对象。我使用了协程,它奏效了。
推荐阅读
- android - 将 sqlcipher 从 3.5.2 升级到 4.0.1 后,GreenDAO 无法访问现有数据库
- internet-explorer - 元素的 SVG appendChild(如路径)不适用于 IE
- php - 来自mysql数据库的网址
- python-3.x - 如何将标签与我的选择列表对齐
- sql-server - 无法在 NVARCHAR 字段中存储特定的 Unicode 代码点/字符
- gojs - 链接验证在 GOJS 图表中返回 false 后图表挂起
- r - 使用 rpart 时找不到“eval 中的错误(predvars,data,env):object `x` not found”的解决方案
- angular - 在 Angular 项目中使用 web api
- mysql - Mysql:如果条件没有给出浮点值的意外值
- reactjs - 两周后反应日期选择器禁用日期