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 /
This question was closed Oct 14, 2020 at 07:17 AM by Catonyourhead for the following reason:

Found the typo

avatar image
0
Question by Catonyourhead · Oct 13, 2020 at 11:36 PM · instantiatedestroyproceduralprefab-instancerng

GameObject not being Destroyed (I've Searched...!)

Okay, so this code was much smaller, but I kept thinking I knew what the problem was and appending it until it became slightly monstrous. So, I generate a grid of cells with another code, adding each to a "cells" 2d array. Each cell has only the top and right wall unless they are on the left or bottom edge of the grid. That code then passes the "Cells" array as well as the array's dimensions to the code below. This is what the code is supposed to do: The "Search" function iterates through each cell and sees if it has been added to the "path" (tracked by the mirrored "pathed" 2d array). If it has not, the "Path" function should link it to a random nearby, already pathed cell and then move out in random diractions, adding cells to the path and updating their status in "pathed". Where I am running into trouble: The random generation seems to work okay when I track it in debug, but the walls which I specify seem to only be deleted by the "linking" section of the function, and not by the "pathing" section of the function. Don't be snide, please. I'm tired. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class SearchPath : MonoBehaviour { public static GameObject NorthWall; public static GameObject SouthWall; public static GameObject EastWall; public static GameObject WestWall; public static GameObject Floor; public static bool[,] Pathed; public static bool[,] Linked;

 public static void Search(GameObject[,] Cells)
 {
     Linked = new bool[Cells.GetLength(0), Cells.GetLength(1)];
     Pathed = new bool[Cells.GetLength(0), Cells.GetLength(1)];
     //Set Bools in mirror grid to false
     for (int X = 0; X < Cells.GetLength(0); X++)
     {
         for (int Z = 0; Z < Cells.GetLength(1); Z++)
         {
             Pathed[X, Z] = false;
         }
     }
     Pathed[0,0] = true;
     //set bools in Linked grid to false
     for (int X = 0; X < Cells.GetLength(0); X++)
     {
         for (int Z = 0; Z < Cells.GetLength(1); Z++)
         {
             Linked[X, Z] = false;
         }
     }
     Linked[0, 0] = true;
     //Search through grid
     for (int X = 0; X < Cells.GetLength(0); X++)
     {
         for (int Z = 0; Z < Cells.GetLength(1); Z++)
         {
             print("Checking if" + X + " " + Z + " has been pathed");
             if (X == 0 && Z == 0)
             {
                 Path(0, 0, Cells);
             }
             else if (Pathed[X, Z] == false)
             {
                 Pathed[X, Z] = true;
             }
             else
             {
                 print("it has been pathed, moving on");
                 Path(X, Z, Cells);
             }
         }
     }
 }
 private static void Path(int X, int Z, GameObject[,] Cells)
 {
     //Only add walls or bordering cells if they exist and are not pathed (circumstantially) Then delete walls as necessary
     NorthWall = null;
     SouthWall = null;
     EastWall = null;
     WestWall = null;
     //North
     if (Cells[X, Z].transform.Find("North").gameObject != null)
     {
         NorthWall = Cells[X, Z].transform.Find("North").gameObject;
     }
     else
     {
         NorthWall = Cells[X, Z + 1].transform.Find("South").gameObject;
     }
     //East
     if (Cells[X, Z].transform.Find("East").gameObject != null)
     {
         EastWall = Cells[X, Z].transform.Find("East").gameObject;
     }
     else
     {
         EastWall = Cells[X + 1, Z].transform.Find("West").gameObject;
     }
     //South
     if (Cells[X, Z].transform.Find("South").gameObject != null)
     {
         SouthWall = Cells[X, Z].transform.Find("South").gameObject;
     }
     else
     {
         SouthWall = Cells[X, Z - 1].transform.Find("North").gameObject;
     }
     //West
     if (Cells[X, Z].transform.Find("West").gameObject != null)
     {
         WestWall = Cells[X, Z].transform.Find("West").gameObject;
     }
     else
     {
         WestWall = Cells[X - 1, Z].transform.Find("East").gameObject;
     }
     //First connect cell to maze
     if (Pathed[X, Z] == false)
     {
         print("linking " + X + ", " + Z);
         bool connected = false;
         while (!connected)
         {
             int rando = (Mathf.CeilToInt(Random.Range(0.0001f, 4)) - 1);
             print(rando);
             switch (rando)
             {
                 case 3:
                     if (Z != Cells.GetLength(1) - 1)
                     {
                         if (Pathed[X, Z + 1] == true)
                         {
                             Destroy(NorthWall);
                             connected = true;
                             Pathed[X, Z] = true;
                             print("delete to link");
                         }
                     }
                     break;
                 case 1:
                     if (Z != 0)
                     {
                         if (Pathed[X, Z - 1] == true)
                         {
                             Destroy(SouthWall);
                             connected = true;
                             Pathed[X, Z] = true;
                             print("delete to link");
                         }
                     }
                     break;
                 case 0:
                     if (X != Cells.GetLength(0) - 1)
                     {
                         if (Pathed[X + 1, Z] == true)
                         {
                             Destroy(EastWall);
                             connected = true;
                             Pathed[X, Z] = true;
                             print("delete to link");
                         }
                     }
                     break;
                 case 2:
                     if (X != 0)
                     {
                         if (Pathed[X - 1, Z] == true)
                         {
                             Destroy(WestWall);
                             connected = true;
                             Pathed[X, Z] = true;
                             print("delete to link");
                         }
                     }
                     break;
                 default:
                     print("something wrong with link");
                     connected = true;
                     break;
             }
         }
     }


     //See if Can Path
     bool canPath = false;

     if (X != 0)
     {
         if (Pathed[X - 1, Z] == false)
         {
             canPath = true;
         }
     }
     if (Z != 0)
     {
         if (Pathed[X, Z - 1] == false)
         {
             canPath = true;
         }
     }
     if (X != Cells.GetLength(0) - 1)
     {
         if (Pathed[X + 1, Z] == false)
         {
             canPath = true;
         }
     }
     if (Z != Cells.GetLength(1) - 1)
     {
         if (Pathed[X, Z + 1] == false)
         {
             canPath = true;
         }
     }
     if (canPath == true) {
         print("Can Continue path");
     }
     else
     {
         print("couldn't path");
     }
     //Stop if top right tile
     if (X == Cells.GetLength(0) - 1 && Z == Cells.GetLength(1) - 1)
     {
         canPath = false;
         print("nevermind, maze end");
     }
     if (canPath == true)
     {
         bool moved = false;
         while (!moved)
         {
             int rando = (Mathf.CeilToInt(Random.Range(0.0001f, 4)) - 1);
             print(rando + " for cutPath");
             switch (rando)
             {
                 case 0:
                     if (X != Cells.GetLength(0) - 1)
                     {
                         if (Pathed[X + 1, Z] == false)
                         {
                             Pathed[X + 1, Z] = true;
                             Destroy(EastWall);
                             moved = true;
                             X++;
                             print("delete to PATH");
                         }
                     }
                     break;
                 case 2:
                     if (X != 0)
                     {
                         if (Pathed[X - 1, Z] == false)
                         {
                             Pathed[X - 1, Z] = true;
                             Destroy(WestWall);
                             moved = true;
                             X--;
                             print("delete to PATH");
                         }
                     }
                     break;
                 case 3:
                     if (Z != Cells.GetLength(1) - 1)
                     {
                         if (Pathed[X, Z + 1] == false)
                         {
                             Pathed[X, Z + 1] = true;
                             Destroy(NorthWall);
                             moved = true;
                             Z++;
                             print("delete to PATH");
                         }
                     }
                     break;
                 case 1:
                     if (Z != 0)
                     {
                         if (Pathed[X, Z - 1] == false)
                         {
                             Pathed[X, Z - 1] = true;
                             Destroy(SouthWall);
                             moved = true;
                             Z--;
                             print("delete to PATH");
                         }
                     }
                     break;
                 default:
                     break;
             }
         }
         if (moved == true)
         {
             print("moving to " + X + " " + Z);
             Path(X, Z, Cells);
         }
     }
 }

}

Comment
Add comment · Show 1
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
avatar image Catonyourhead · Oct 13, 2020 at 09:46 PM 0
Share

Here is my gridmaker class. Each cell is equipped with four walls and a floor on instantiation.

public class Grid$$anonymous$$aker : $$anonymous$$onoBehaviour { private GameObject CellPrefab; private GameObject Cell; private GameObject NorthWall; private GameObject SouthWall; private GameObject EastWall; private GameObject WestWall; private GameObject Floor; private GameObject[,] Cells; // Start is called before the first frame update void Awake() { CellPrefab = Resources.Load("Art/Prefabs/Architecture/Cell") as GameObject; }

 void Update()
 {
     
 }

 public GameObject[,] $$anonymous$$akeGrid(int height, int width) {
     Cells = new GameObject[width,height];
     for (int x = 0; x < width; x++) {
         for (int z = 0; z < width; z++)
         {
             Cell = Instantiate(CellPrefab, new Vector3(x * 6, 0, z * 6), new Quaternion(0, 0, 0, 0));
             if (z != height - 1)
             {
                 NorthWall = Cell.transform.Find("North").gameObject;
                 Destroy(NorthWall);
             }
             if (x != width - 1)
             {
                 EastWall = Cell.transform.Find("East").gameObject;
                 Destroy(EastWall);
             }
             Cells[x,z] = Cell;
         }
     }
     return Cells;
 }

}

I seriously love you in advance

2 Replies

  • Sort: 
avatar image
1

Answer by rh_galaxy · Oct 14, 2020 at 07:05 AM

I'm not sure, but whenever you destroy a wall, you should set your gameobject variable to null after. I haven't followed the recursive code enough so I don't know if you check/use the gameobjects any time after they are destroyed. If you do you will think they are valid when infact it has been or will be destroyed. So:

 Destroy(EastWall); //this destroyes the gameObject later but not immediatly
 EastWall = null; //so you don't think it exists later
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
avatar image
0

Answer by Catonyourhead · Oct 14, 2020 at 04:59 AM

alt textBump. I feel as though my problem isn't with the logic, but with some way I'm calling a variable wrong or something... This is my third project ever, so there's a lot of gaps in my knowledge. EDIT: I have done more testing and when a cell doesn't have a north/south/etc wall it sets its wall to the cell next to it's inverse wall. at least that's waht's supposed to happen. It appears that my problem isn't the pathing, as with the debugs I can actually follow the logic around the maze and it should be working and making aperfect maze, but somehow, from printing the names of the walls it's trying to delete, I realize that it's trying TO DELETE WALLS THAT DON'T EXIST? I print the name of the wall, but shouldn't it print "null" if there is no wall there? I will seriously send you my project file if you can help. Here is a screenshot. This is the result of me printing the name of the wall. you can see, that wall doesn't actually exist. if I go around the editor deleting walls, I can see there is only one wall on each "edge/link." Please help!!!


capture.jpg (130.2 kB)
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

Follow this Question

Answers Answers and Comments

183 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 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

Instantiating too much at Start? Terrain Objects. 0 Answers

Destroy instance 0 Answers

Do empty transforms take up much CPU power? 1 Answer

Destroying Instantiated Obect Problem 1 Answer

How to spawn an object continually while floating vertically like a bubble 1 Answer


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