Home Game Development Unity Nested FOR loops Bug

Unity Nested FOR loops Bug

0
Unity Nested FOR loops Bug

[ad_1]

I’m writing a script to randomly generate a maze, and I can not even get it off the bottom, rip. I’ve written fractal turbines so I’m no stranger to nested FOR loops. FOR x{ FOR y }
I’m utilizing my standard methods to generate the maze. To initialize it, I set the boundary of the Maze (NESW sides) to be partitions like so:

// Create
List<Room> row = new List<Room>();
for(int i=0; i<width; i++)  // Columns (X-axis)
{   row.Clear();
    for(int j=0; j<depth; j++){ // Rows (Z-axis)
        row.Add(new Room(i, yOffset, j));

        if(i==width-1){ Debug.Log("SetE: i "+i+", j "+j);
        row[j].setE(-1); }
        if(i==0){ Debug.Log("SetW: i "+i+", j "+j);
        row[j].setW(-1); }
    }
    row[width-1].setN(-1);
    row[0].setS(-1);
    rooms.Add(row);
}
// Instantiate
for(int i=0; i<width; i++) for(int j=0; j<depth; j++)
{   // Room
    Instantiate(roomGO, new Vector3(i + rework.place.x,
        yOffset + rework.place.y, j + rework.place.z), rework.rotation);
    // North Side
    Instantiate(getWall(rooms[i][j],0), new Vector3(i + rework.place.x,
        yOffset + rework.place.y, j + rework.place.z + .75f), rework.rotation);
    // East Side
    Instantiate(getWall(rooms[i][j],1), new Vector3(i + rework.place.x + .75f,
        yOffset + rework.place.y, j + rework.place.z), rework.rotation);
    // South Side
    Instantiate(getWall(rooms[i][j],2), new Vector3(i + rework.place.x,
        yOffset + rework.place.y, j + rework.place.z - .75f), rework.rotation);
    // West Side
    Instantiate(getWall(rooms[i][j],3), new Vector3(i + rework.place.x - .75f,
        yOffset + rework.place.y, j + rework.place.z), rework.rotation);
}

My downside is right here (*I feel), Lines 7 to 10:

if(i==width-1){ Debug.Log("SetE: i "+i+", j "+j);
    row[j].setE(-1); }
if(i==0){ Debug.Log("SetW: i "+i+", j "+j);
    row[j].setW(-1); }

The positions are appropriate. I’ve no issues with the North and South sides.
The Western partitions are usually not Instantiating. But the Eastern partitions are Instantiating on each single row?
I’m attempting to set all of the West and East partitions of the rooms with Coords (x=0/x=Width, z). The Debug.Log strains are printing the anticipated coordinates.

Question
Why aren’t the Eastern and Western partitions Instantiating correctly? Is this Unity, C#, my Code, or my Computer? What is a greater strategy to set the partitions of my Maze?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here