Ok so I’m trying to simplify a random level generator. in my scene I have a couple rooms which will be called to instantiate at a certain position when the trigger is passed. The generation code works fantastic but it all get’s added to a list that I then want to destroy the gameobject at index 0 of the list when the index gets to 3. I can seem to remove it just fine from the list but no matter what I try I can’t seem to destroy that game object.
here’s my script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class IRGenScript : MonoBehaviour {
public GameObject[] availableRooms;
public List<GameObject> currentRooms;
public GameObject roomtoRemove;
private float roomWidth;
private int myLayerMask = 1 << 10;
private Vector3 positionRoom;
void Start ()
{
}
void Update()
{
}
void OnTriggerExit2D (Collider2D generator)
{
Destroy (generator.gameObject);
RaycastHit2D hitdown = Physics2D.Raycast ((GameObject.Find ("Camera target").transform.position) , Vector2.down,Mathf.Infinity, myLayerMask);
if (hitdown.collider != null)
positionRoom.y = hitdown.collider.transform.position.y;
if (generator.transform.tag == "Room End") {
int randomRoomIndex = Random.Range (0, availableRooms.Length);
GameObject room = (GameObject)Instantiate (availableRooms [randomRoomIndex]);
room.transform.position = new Vector2 (transform.position.x, positionRoom.y + 2);
currentRooms.Add (room);
List<GameObject> roomsToRemove = new List<GameObject> ();
if (currentRooms.Count.Equals (3))
currentRooms.RemoveAt (0);
}
}
}
and it’s connected to the player.