Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GlitteringStone · Dec 22, 2012 at 12:30 PM · c#arrayrangemaze

Array Index Out of Range - Maze Generation Algorithm

I'm trying to write a maze generation algorithm, I'm basically copying it from one I found online and translating it into C#. It compiles perfectly fine, but when I run the program in Unity, the error log pretty much explodes with errors saying:

IndexOutOfRangeException: Array index is out of range.

mazeGenerateScript.generateMaze () (at Assets/Scripts/mazeGenerateScript.cs:19) mazeGenerateScript.Update () (at Assets/Scripts/mazeGenerateScript.cs:109) I can't figure out why it does this. Here's my code, the relevant line is the "maze[i,j] = 1;" in the beginning of generateMaze(); using UnityEngine; using System.Collections;
public class mazeGenerateScript : MonoBehaviour {
public static int height = 15; public static int width = 20; public Transform wallSegment; public int[,] maze = new int[,] { {height}, {width} }; // Use this for initialization void Start () {
}
public int[,] generateMaze() { // Initialize for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) maze[i,j] = 1;
// r for row, c for column // Generate random r int r = Random.Range(0,height); while (r % 2 == 0) { r = Random.Range(0,height); } // Generate random c int c = Random.Range(0,width); while (c % 2 == 0) { c = Random.Range(0,width); } // Starting cell maze[r,c] = 0;
// Allocate the maze with recursive method recursion(r, c);
return maze; }
public void recursion(int r, int c) { // 4 random directions int[] randDirs = generateRandomDirections(); // Examine each direction for (int i = 0; i < randDirs.Length; i++) { switch(randDirs[i]) { case 1: // Up // Whether 2 cells up is out or not if (r - 2 <= 0) continue; if (maze[r - 2,c] != 0) { maze[r - 2,c] = 0; maze[r - 1,c] = 0; recursion (r - 2, c); } break; case 2: // Right // Whether 2 cells to the right is out or not if (c + 2 >= width - 1) continue; if (maze[r,c + 2] != 0) { maze[r,c + 2] = 0; maze[r,c + 1] = 0; recursion(r, c + 2); } break; case 3: // Down // Whether 2 cells down is out or not if (r + 2 >= height - 1) continue; if (maze[r + 2,c] != 0) { maze[r+2,c] = 0; maze[r+1,c] = 0; recursion(r + 2, c); } break; case 4: // Left // Whether 2 cells to the left is out or not if (c - 2 <= 0) continue; if (maze[r,c - 2] != 0) { maze[r,c - 2] = 0; maze[r,c - 1] = 0; recursion(r, c - 2); } break; }
}
}
public int[] generateRandomDirections() { int[] randoms = new int[4];
for (int t = 0; t < randoms.Length; t++ ) { int tmp = randoms[t]; int r = Random.Range(t, randoms.Length); randoms[t] = randoms[r]; randoms[r] = tmp; } return randoms; }
// Update is called once per frame void Update () { int xCoordinate = 0; int yCoordinate = 0;
int[,] masterMaze = new int[,] { {height}, {width} }; masterMaze = generateMaze();
for (int a = 0; a < masterMaze.GetLength(0); a++) { for (int b = 0; b < masterMaze.GetLength(1); b++) { if (masterMaze[a,b] == 1) { Instantiate(wallSegment, new Vector3(xCoordinate, yCoordinate, 8), Quaternion.identity); xCoordinate = xCoordinate + 16; } else xCoordinate = xCoordinate + 16; } yCoordinate = yCoordinate + 16; }
} } I tried reducing the number of iterations in that initialization loop, but that didn't seem to work, and I can't think of anything else that would cause this error, does anyone have any idea?
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dave-Carlile · Dec 22, 2012 at 01:02 PM

You're not initializing the maze array correctly.

 public int[,] maze = new int[,] { {height}, {width} };

That allocates enough room for two items and initializes the values to height and width. You want to create the array with enough froom to store height width* values.

public int[,] maze = new int[height, width];

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Random Material 0 Answers

NullReferenceException - Maze Generator Script 1 Answer

How to deactivate all GameObject in a array, except last one 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges