Trouble with Arrays

I’m new to unity but consider myself fairly familiar with c#, which I’m using for scripting in unity. I’m making a very simple maze generator, which draws cubes in place of walls, whilst leaving “path” areas empty. The program always breaks in runtime, after drawing the first column of cubes. Here is my code, I’m sure it’s something stupid that I’ve missed but I just can’t get past it!

using UnityEngine;
using System.Collections;

public class MazeGenerator : MonoBehaviour {

public int mazeHeight = 11;
public int mazeWidth = 11;
private int[,] Maze;

public GameObject wallGo;

private static System.Random _rand = new System.Random();

// Use this for initialization
void Start () {
	//Run mazegen
	Maze = GenerateMaze (mazeHeight, mazeWidth);

	//Show a visual representation of maze within unity
	for (int i = 0; i < mazeHeight; i++)
	{
		for (int j = 0; i < mazeWidth; j++)
		{
			*if (Maze[i,j] == 1)*
			{
				//Create position for cube(wall)
				Vector3 pos = new Vector3(i, 0, j);

				//Create wall within the scene
				GameObject wall = Instantiate(wallGo) as GameObject;

				if (wall != null)
					wall.transform.position = pos;
			}
		}
	}
}

private int[,] GenerateMaze (int height, int width)
{
	//Create our temporary maze
	int[,] maze = new int[height, width];

	//Initialise all cells to 1
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			maze[i,j] = 1;
		}
	}

	//Generate a new random seed
	System.Random rand = new System.Random ();

	//Choose a random cell to start digging from
	int r = rand.Next (height);
	while (r % 2 == 0)
					r = rand.Next (height);

	int c = rand.Next (width);
		while (c % 2 == 0)
					c = rand.Next (width);

	//Set starting cell value to 0
	maze [r, c] = 0;

	//Create maze using depth first search algorithm
	MazeDigger (maze, r, c);

	//Return maze
	return maze;
}

private void MazeDigger(int[,] maze, int r, int c)
{
	//Create possible dig directions
	//1 - North
	//2 - South
	//3 - East
	//4 - West

	int[] directions = new int[] {1, 2, 3, 4};
	Shuffle (directions);

	//Look in new direction, two blocks ahead
	for (int i = 0; i < directions.Length; i++)
	{
		switch(directions*)*
  •   	{*
    
  •   		case 1:* 
    
  •   			if (r - 2 <= 0)*
    
  •   			continue;*
    
  •   		if (maze[r-2, c] != 0)*
    
  •   		{*
    
  •   			maze[r-2, c] = 0;*
    
  •   			maze[r-1, c] = 0;*
    
  •   			MazeDigger(maze, r-2, c);*
    
  •   		}*
    
  •   		break;*
    
  •   		case 2:*
    
  •   			if (c + 2 >= mazeWidth - 1)*
    
  •   			continue;*
    
  •   		if (maze[r, c+2] != 0)*
    
  •   		{*
    
  •   			maze[r, c+2] = 0;*
    
  •   			maze[r, c+1] = 0;*
    
  •   			MazeDigger (maze, r, c+2);*
    
  •   		}*
    
  •   		break;*
    
  •   		case 3:*
    
  •   			if (r + 2 >= mazeHeight - 1)*
    
  •   			continue;*
    
  •   		if (maze[r+2, c] != 0)*
    
  •   		{*
    
  •   			maze[r+2, c] = 0;*
    
  •   			maze[r+1, c] = 0;*
    
  •   			MazeDigger (maze, r+2, c);*
    
  •   		}*
    
  •   		break;*
    
  •   		case 4:*
    
  •   			if (c - 2 <= 0)*
    
  •   			continue;*
    
  •   		if (maze[r, c-2] != 0)*
    
  •   		{*
    
  •   			maze[r, c-2] = 0;*
    
  •   			maze[r, c-1] = 0;*
    
  •   			MazeDigger (maze, r, c-2);*
    
  •   		}*
    
  •   		break;*
    
  •   	default:*
    
  •   			break;*
    
  •   	}*
    
  •   }*
    
  • }*

  • public static void Shuffle(T array)*

  • {*

  •   var random = _rand;*
    
  •   for (int i = array.Length; i > 1; i--)*
    
  •   {*
    
  •   	int j = random.Next (i);*
    
  •   	T tmp = array[j];*
    
  •   	array[j] = array[i-1];*
    
  •   	array[i-1] = tmp;*
    
  •   }*
    
  • }*

  • // Update is called once per frame*

  • void Update () {*

  • }*
    }
    Line 19, in the start function, “if (Maze[i,j] == 1)” is where the error lies, the message is “Array index is out of range.”

2 Answers

2

I believe it is because Maze is never initialized before you are setting the value.

You could try changing line 12 to:

Maze  = new int[mazeHeight, mazeWidth];
Maze = GenerateMaze(mazeHeight, mazeWidth);

Or change line 12 to:

GenerateMaze(Maze, mazeHeight, mazeWidth);

and line 34-37 to:

private void GenerateMaze(int[,] maze, int height, int width)
{
    //Create our temporary maze
    maze = new int[height, width];

Thanks for the feedback guys, I tried both options but had no luck. It turns out it was something much less complex, I’d used i instead of j on line 17!