I am Making a game where I need rooms to be generated when the door to the next is opened and then once 3 rooms have been generated the room 3 rooms back needs to be deleted, here is my code for deleting the third back room:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomDeGenarator : MonoBehaviour
{
private float RoomsOpened = 0;
public void DoorCount(){
RoomsOpened ;
if(RoomsOpened > 3.0f){
Destroy(gameObject);
}
}
}
every time a door is opened to the next room it generate the next, so every time a new room is generated it adds 1 to "RoomsOpened", the code works fine its just that when it deletes the game object it deletes the original one instead of the clone
so, how do I delete the clone instead of the original gameobject?
CodePudding user response:
Keep your room in a list, add them on creation. When your list count is 4, destroy first item
List<Room> rooms = new List<Room>();
void OnRoomCreate()
{
Room newRoom = CreateRoom();
rooms.Add(newRoom);
if (rooms.Count >= 4)
{
Destroy(list[0]);
}
}
CodePudding user response:
Instantiate object by giving it variable name.
Gameobject room = Instantiate(roomPrefab, Vector3.zero, Quaternion.idendity);
Then you can say Destroy(room);