我试图用立方体生成一个平坦的世界,我有几个嵌套的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.
更多推荐
发布评论