Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Need help with Procedural generation

Discussion in 'Scripting' started by ArVilla21, May 10, 2024.

  1. ArVilla21

    ArVilla21

    Joined:
    May 8, 2023
    Posts:
    4
    So I am making this game where at the beginning some premade small rooms get selected and the game makes them into one big map, but i although the rooms do get created, the spawn infinetely and the spawn on top of each other,
    my script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class roomSpawner : MonoBehaviour
    {
    /* 1 -> bottom
    2 -> top
    3 -> left
    4 -> right
    */
    //Publics
    public int openingDirection;
    //Privates:
    private bool spawned = false;
    private roomTemplates templates;
    private int rand;
    void Start()
    {
    templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<roomTemplates>();
    Invoke("Spawn", 1f);
    }
    void Spawn()
    {
    if (spawned == false)
    { if (openingDirection == 1)
    {
    // Spawn a BOTTOM door.
    rand = Random.Range(0,templates.bottomRooms.Length);
    Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation); // Instantiate = Spawn

    }
    else if (openingDirection == 2)
    {
    // Spawn a TOP door.
    rand = Random.Range(0, templates.topRooms.Length);
    Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation); // Instantiate = Spawn

    }
    else if (openingDirection == 3)
    {
    // Spawn a LEFT door.
    rand = Random.Range(0, templates.leftRooms.Length);
    Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation); // Instantiate = Spawn
    }
    else if (openingDirection == 4)
    {
    // Spawn a RIGHT door.
    rand = Random.Range(0, templates.rightRooms.Length);
    Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation); // Instantiate = Spawn

    }
    spawned = true;
    }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.CompareTag("SpawnPoint"))
    {
    Destroy(gameObject);
    }
    }

    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,696
    FYI: I removed the "2D physics" tag as your post is not related to 2D physics at all.
     
  3. ArVilla21

    ArVilla21

    Joined:
    May 8, 2023
    Posts:
    4
    Oh , oops
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,409
    Sounds like you wrote a bug... that means it is... Time to start debugging!

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(...);
    statements to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.



    Nobody here reads unformatted code. If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/



    If you learn more and things are still mysterious to you...

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     
  5. ArVilla21

    ArVilla21

    Joined:
    May 8, 2023
    Posts:
    4
    Thanks! It is the first time I post on the unity forum so I really appreciate it!
     
  6. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    How many instances of roomSpawner do you have in the scene?
    To debug, you can add
    void OnEnable(){...} 
    with a
    Debug.Log
    in the script.
    Then you can find out where/when the room spawners are being created.
     
  7. ArVilla21

    ArVilla21

    Joined:
    May 8, 2023
    Posts:
    4
    I have one in every opening of the room except from the starter one that has one in the middle as well