- Home /
I can't get it to work
Im new to programing and im trying to make a dungeon generater, as something small to start out with. but it will not work
this is the code I use the first is the RoomSpawner script and the secend is the script that holds the prefabs of the Rooms
using UnityEngine; using Random = UnityEngine.Random; using System; using System.Collections; using System.Collections.Generic; using System.Security.Cryptography;
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 rand;
private bool spawned = false;
void start()
{
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
Invoke("Spawn", 0.1f);
}
void Spawn()
{
if (spawned == false)
{
if (openingDirection == 1)
{
// need to spawn a room with a bottom door
rand = Random.Range(0, templates.bottomRooms.Length);
Instantiate(templates.bottomRooms[rand], transform.position, Quaternion.identity);
}
else if (openingDirection == 2)
{
// need to spawn a room with top door
rand = Random.Range(0, templates.topRooms.Length);
Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
}
else if (openingDirection == 3)
{
// need to spawn a room with a left door
rand = Random.Range(0, templates.leftRooms.Length);
Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
}
else if (openingDirection == 4)
{
// need to spawn a room with a right door
rand = Random.Range(0, templates.rightRooms.Length);
Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
}
spawned = true;
}
}
private void OnTriggerEnter2D(Collider2D other){
if (other.CompareTag("SpawnPoint") && other.GetComponent<RoomSpawner>().spawned == true)
{
Destroy(gameObject);
}
}
}
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;
} ,Im new to this and i have tried to follow some on yt (youtube) to find my problem but i can figur it out
this is my code Im trying to make a dungeon generater
using UnityEngine; using Random = UnityEngine.Random; using System; using System.Collections; using System.Collections.Generic; using System.Security.Cryptography;
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 rand;
private bool spawned = false;
void start()
{
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
Invoke("Spawn", 0.1f);
}
void Spawn()
{
if (spawned == false)
{
if (openingDirection == 1)
{
// need to spawn a room with a bottom door
rand = Random.Range(0, templates.bottomRooms.Length);
Instantiate(templates.bottomRooms[rand], transform.position, Quaternion.identity);
}
else if (openingDirection == 2)
{
// need to spawn a room with top door
rand = Random.Range(0, templates.topRooms.Length);
Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
}
else if (openingDirection == 3)
{
// need to spawn a room with a left door
rand = Random.Range(0, templates.leftRooms.Length);
Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
}
else if (openingDirection == 4)
{
// need to spawn a room with a right door
rand = Random.Range(0, templates.rightRooms.Length);
Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
}
spawned = true;
}
}
private void OnTriggerEnter2D(Collider2D other){
if (other.CompareTag("SpawnPoint") && other.GetComponent<RoomSpawner>().spawned == true)
{
Destroy(gameObject);
}
}
}
and i use this code to hold the prefabs
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;
}
Your answer