- Home /
Question by
thiagoferos9 · Jul 27, 2021 at 05:20 AM ·
tilemaptilesprocedural generationprocedural-generation
My randomize rooms script is giving an error.
I'm creating a game of dungeons to study programming, but my script is giving an error and I can't fix it, here's my script:
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 rand;
public bool spawned = false;
public float waitTime = 3.5f;
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, templates.bottomRooms[rand].transform.rotation);
}
else if (openingDirection == 2)
{
// Need to spawn a room with a 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;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("SpawnPoint"))
{
if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false)
{
Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
Destroy(gameObject);
}
spawned = true;
}
}
}
and the other:
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[] Chest;
public GameObject closedRoom;
public float wait;
public List<GameObject> rooms;
public float waitTime;
private bool spawnedBoss;
public GameObject boss;
void Update()
{
if (waitTime <= 0 && spawnedBoss == false)
{
for (int i = 0; i < rooms.Count; i++)
{
if (i == rooms.Count - 1)
{
Instantiate(boss, rooms[i].transform.position, Quaternion.identity);
spawnedBoss = true;
}
}
}
else
{
waitTime -= Time.deltaTime;
}
}
}
Comment