Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by borisvdb · Dec 05, 2017 at 04:55 PM · randomnullreferenceexceptionalgorithmmazerng

Perfect Maze Generator error

Hi, everyone.

I'm trying to make a perfect maze generator in unity3d but I'm getting the object reference not set to an instance of an object error.

The script is also supposed to create cells and assign the walls to north/west/south/east but has only parented the cloned walls to GameObject wallRecord ('Maze' in the higherarchy) so far. I think i've made a mistake in the math somewhere.

Any help would hugely appreciated, thank you.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MazeManager : MonoBehaviour {
     
     [System.Serializable]
 
     //Cell Variables
     public class Cell {
         public bool visited;
         public GameObject north;
         public GameObject east;
         public GameObject south;
         public GameObject west;
         public Cell (){
 
             north = null;
             east = null;
             west = null;
             south = null;
         //End
         }
     }
 
     public GameObject wall;
     public int xsize = 11;
     public int ysize = 7;
     public int currentCell = 0;
 
     private int totalcells = 0;
     public Cell[] cells;
     private float wallLength = 1f;
     private Vector3 initialPos;
     private GameObject wallRecord;
     private GameObject tempWall;
 
     // Use this for initialization
     void Start () {
         Createwall ();
     
     }
 
     void Createwall () {
 
         wallRecord = new GameObject ();
         wallRecord.name = "Maze";
 
         initialPos = new Vector3 ((-xsize / 2) + wallLength / 2, 0.0f, (-ysize / 2) + wallLength / 2);
         Vector3 myPos = initialPos;
 
         //x-axis walls
 
             for (int i = 0; i < ysize; i++) {
                 for (int j = 0; j <= xsize; j++) {
                     myPos = new Vector3 (initialPos.x + (j*wallLength) -wallLength / 2, 0.0f, initialPos.z + (i*wallLength) -wallLength / 2);
 
                 tempWall = Instantiate (wall, myPos, Quaternion.identity) as GameObject;
                     tempWall.transform.parent = wallRecord.transform;
             }
         }
 
         //y-axis walls
 
             for (int i = 0; i <= ysize; i++) {
                 for (int j = 0; j < xsize; j++) {
                     myPos = new Vector3 (initialPos.x + (j*wallLength), 0.0f, initialPos.z + (i*wallLength) -wallLength);
 
                     tempWall = Instantiate (wall, myPos, Quaternion.Euler (0.0f, 90.0f, 0.0f)) as GameObject;
                     tempWall.transform.parent = wallRecord.transform;
             }
         }
                 CreateCells ();
     }
 
     void CreateCells () {
 
         GameObject[] allWalls;
         int children = wallRecord.transform.childCount;
         allWalls = new GameObject [children];
         cells = new Cell[xsize * ysize];
         int EWProcess = 0;
         int childProcess = 0;
         int termCount = 0;
 
         //Gets all the children
 
             for (int i = 0; i < children; i++) {
                 allWalls[i] = wallRecord.transform.GetChild(i).gameObject;
         }
 
         //Walls to Cells
 
         if (termCount == xsize) {
 
             for (int cellprocess = 0; cellprocess < cells.Length; cellprocess++) {
                 cells [cellprocess] = new Cell ();
                 cells [cellprocess].east = allWalls [EWProcess];
                 cells [cellprocess].south = allWalls [childProcess + (xsize + 1) * ysize];
 
                 if (termCount == xsize) {
                     EWProcess += 2;
                     termCount = 0;
 
                     }
                     else
                         EWProcess++;
                         termCount++;
                         childProcess++;
                         cells [cellprocess].west = allWalls [EWProcess];
                         cells [cellprocess].north = allWalls [(childProcess+ (xsize+1)*ysize) +xsize - 1];
 
                         }
                     }
         CreateMaze ();
                 }
 
     void CreateMaze () {
 
         RecordNeighbour ();
 
     }
 
     void RecordNeighbour () {
 
         totalcells = xsize * ysize;
         int Length = 0;
         int[] neigbours = new int[4];
         int rec = 0;
 
         rec = ((currentCell + 1) / xsize);
         rec -= 1;
         rec *= xsize;
         rec += xsize;
 
         //west
         if (currentCell + 1 < totalcells && (currentCell) != rec) {
             if (cells [currentCell + 1].visited == false) {
                 neigbours [Length] = currentCell + 1;
                 Length++;
             }
         }
         //east
         if (currentCell - 1 < totalcells && (currentCell) != rec) {
             if (cells [currentCell - 1].visited == false) {
                 neigbours [Length] = currentCell - 1;
                 Length++;
             }
         }
         //north
         if (currentCell + xsize < totalcells) {
             if (cells [currentCell + xsize].visited == false) {
                 neigbours [Length] = currentCell + xsize;
                 Length++;
             }
         }
         //south
         if (currentCell - xsize < totalcells) {
             if (cells [currentCell - xsize].visited == false) {
                 neigbours [Length] = currentCell - xsize;
                 Length++;
             }
         }
         for (int i = 0; i < Length; i++)
             Debug.Log (neigbours [i]);
     }
 }


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

0 Replies

· Add your reply
  • Sort: 

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

123 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Only instantiate random seed once 1 Answer

how to build a complex 3d random maze generator? 2 Answers

How Can I Jump Random Places Without Overlapping? 0 Answers

How to randomly create a tile based maze 0 Answers

How can you do borderlands style random weapons and stat generation? 2 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