smt
smt

Reputation: 1

How do I delete a clone game object instead of the original gameobject?

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?

Upvotes: 0

Views: 209

Answers (2)

Feryadi Dağcı
Feryadi Dağcı

Reputation: 11

Instantiate object by giving it variable name.

GameObject room = Instantiate(roomPrefab, Vector3.zero, Quaternion.identity);

Then you can say Destroy(room);

Upvotes: 0

Everts
Everts

Reputation: 10721

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]);
      }
}

Upvotes: 1

Related Questions