Unity冻结运行,循环实例化(Unity freeze on run, looped Instantiate)

编程入门 行业动态 更新时间:2024-10-28 16:27:39
Unity冻结运行,循环实例化(Unity freeze on run, looped Instantiate)

我试图用立方体生成一个平坦的世界,我有几个嵌套的for循环来遍历坐标并放置每个块。 当我运行应用程序时,统一游戏引擎会冻结。 从我所知道的循环是正确的应该工作并正确退出。 我原本以为也许我试图实例化太多的对象,所以我大大减少了数量,但我仍然遇到同样的问题。 我一直在盯着这段代码一段时间而找不到问题,所以我想知道是否有人知道这个问题是什么?

using UnityEngine; using System.Collections; public class Chunk : MonoBehaviour { public readonly int chunkWidth = 2; public readonly int chunkHeight = 2; public GameObject TestBlock; // Use this for initialization void Start () { spawnChunk (0,0); } // Update is called once per frame void Update () { } public void spawnChunk(int posX, int posZ){ for(int x = 0; x <= chunkWidth; x++){ for(int y =0; y <= chunkHeight; y++){ for(int z = 0; x <= chunkWidth; z++){ //create object Instantiate(TestBlock, new Vector3(x + posX, y, z + posZ), Quaternion.identity); } } } } }

I'm trying to generate a flat world out of cubes, I have several nested for loops to cycle through the coordinates and to place each block. The unity game engine freezes when I run the application. From what I can tell the loops are correct are should work and exit properly. I originally thought maybe I was trying to instantiate too many objects so I reduced the amount considerably, but I still get the same issue. I've been staring at this code for a while now and can't find an issue, so I was wondering if anyone knows what this issue is?

using UnityEngine; using System.Collections; public class Chunk : MonoBehaviour { public readonly int chunkWidth = 2; public readonly int chunkHeight = 2; public GameObject TestBlock; // Use this for initialization void Start () { spawnChunk (0,0); } // Update is called once per frame void Update () { } public void spawnChunk(int posX, int posZ){ for(int x = 0; x <= chunkWidth; x++){ for(int y =0; y <= chunkHeight; y++){ for(int z = 0; x <= chunkWidth; z++){ //create object Instantiate(TestBlock, new Vector3(x + posX, y, z + posZ), Quaternion.identity); } } } } }

最满意答案

在你最深的for循环中, x总是小于chunkWidth ,导致无限循环; z没有被检查任何东西。

更改:

for(int z = 0; x <= chunkWidth; z++){

至:

for(int z = 0; z <= chunkWidth; z++){

Unity应该不再冻结。

In your deepest for loop, x will always be less than chunkWidth, causing an infinite loop; z isn't being checked against anything.

Change:

for(int z = 0; x <= chunkWidth; z++){

to:

for(int z = 0; z <= chunkWidth; z++){

and Unity should no longer freeze.

更多推荐

本文发布于:2023-08-03 01:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   freeze   Unity   Instantiate   looped

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!