Destroying an Object with no collision?

Hey, I’ve struggling for several hours with this issue and now decided to ask for help. I will provide my scripts and screenshots to give the best possible overview.

I am working on a dungeon generator which works pretty good, but there are two main issues. I have these dead ends (Screenshot by Lightshot) which allow the player to enter the Gamescene. To tackle that, I generate a box that blocks these openings (Screenshot by Lightshot), but this box creates the following problems:

  • I can’t set a maximum number of spawned rooms. The box is being instanced from the same script as the other rooms, which means, that the maximum number also applies to the box. This is extremely annoying to me and I don’t know how to solve that.
  • It’s not clean at all. It would be sooo much better if the wrong rooms with openings (all rooms are prefabs) would destroy and replace themselves. I already tried working with raycasts but I didn’t manage to fix it.

Before posting here I really wanted to solve these issues by myself, but I am at the very beginning of programming with C# and Unity so my knowledge and experience is very limited which makes it really difficult to me.

I am using the following scripts:

[RoomSpawner → spawns rooms and the box]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomSpawner : MonoBehaviour
{
    public int openingDirection;

    /*
        1 -> need bottom door
        2 -> need top door
        3 -> need left door
        4 -> need right door
    */

    private RoomTemplates templates;
    private int random;
    public bool spawned = false;


    static int maxRooms;
   


    void Start ()
    {
        templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
        StartCoroutine(SpawnRooms());
    }


     IEnumerator SpawnRooms()
    {
        yield return new WaitForSeconds(0.1f);
        Spawn();
    }
   

    IEnumerator FixOpenRooms()
    {

                yield return new WaitForSeconds(1.5f);
                Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
                Destroy(gameObject);
                Debug.Log("Fixed!");
    

       
    }



    void Spawn ()
    {

        if (spawned == false) // && maxRooms < 15)
        {

            if (openingDirection == 1)             // Spawn Bottom Door

            {
                random = Random.Range(0, templates.bottomRooms.Length);
                Instantiate(templates.bottomRooms[random], transform.position, Quaternion.identity);
                //maxRooms++;
            }
            else if (openingDirection == 2)        // Spawn Top Door

            {
                random = Random.Range(0, templates.topRooms.Length);
                Instantiate(templates.topRooms[random], transform.position, Quaternion.identity);
                //maxRooms++;

            }
            else if (openingDirection == 3)        // Spawn Left Door

            {
                random = Random.Range(0, templates.leftRooms.Length);
                Instantiate(templates.leftRooms[random], transform.position, Quaternion.identity);
                //maxRooms++;

            }
            else if (openingDirection == 4)        // Spawn Right Door

            {
                random = Random.Range(0, templates.rightRooms.Length);
                Instantiate(templates.rightRooms[random], transform.position, Quaternion.identity);
                //maxRooms++;


            }

            spawned = true;
        }

      
    }

   void OnTriggerEnter2D(Collider2D other)
    {
        if(other.CompareTag("SpawnPoint"))
        {
            if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false)
            {

                StartCoroutine(FixOpenRooms());

            }
            spawned = true;

        }
    }
   


}

[RoomTemplates → Array that contains all Rooms]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomTemplates : MonoBehaviour
{
    public GameObject[] bottomRooms;
    public GameObject[] topRooms;
    public GameObject[] leftRooms;
    public GameObject[] rightRooms;

    public GameObject closedRoom;
}

[Destroyer → Prevents the box from spawning at the entry room]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Destroyer : MonoBehaviour
{

    private RoomTemplates templates;

    void OnTriggerEnter2D(Collider2D other)
    {
        if (!other.CompareTag("Player") && !other.CompareTag("ClosedRoom"))
        {

            templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
            Destroy(other.gameObject);

        }
    }
}

I appreciate any kind of help! Thank you!

First of all, I love it. These sorts of things are awesome fun to code up.

BUT… I have to warn you, some of the laying-out logic can be tricky.

Looking through your scripts it will not be possible to reason about what you want vs what you are getting. That will have to be done by you, but here are some ideas:

  • make the dungeon SUPER small, whatever the smallest thing is that has a chance of showing your problem.

  • try to emit lots of Debug.Log() info about what it’s doing at each step.

  • see if you can find the logic issue, IF that is what it is. Keep in mind it might just be a spatial issue with how you are generating the dungeon that does not account for dead ends properly, and may not ultimately be able to the way it is written.

Finally let me leave you a link to this fellow, who has written some interesting bits on dungeon generation.

https://journal.stuffwithstuff.com/2014/12/21/rooms-and-mazes/

There are plenty other writers out on the interwebs, and the art and science of dungeon generation spans mathematics of graph theory, all the way to the artistic “coolness” factor that you feel when you enter it.

1 Like

I agree, but it is also really difficult as a beginner haha!

First of all, thanks for your response and the link, I will have a look at it right after this post.

I acutally re-wrote the whole system and made it way simpler. Instead of my complex room-cluster that consisted of 14 different rooms with opening, I am now using an own grid system which sets a new coordinate for each room spawned.

The doors are being created by shooting a raycast to the outside of the box, and if it collides with another object tagged as “Door”, it destroys this object.

My new system is so much easier to read and handle, it’s really awesome! Now I want to figure out how to spawn different sized rooms, without making them collide with each other- I am wondering if I can figure out a working method.

1 Like