How can I destroy a Gameobject from a list at a specific index
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.
as well my player has a gameobject pasted to it for the camera target. I did this because I'm using free floating sprites to represent the player and if I don't then the camera seems to ignore most of them I'm aware of how to fix it so no worries I just find it a bit easier this way
You're first calling Destroy on the generator object and then You check it's tag (lines 21 and 26). to destroy the room just Call Destroy(currentRooms[index]) before removing it from list (line 33).
Answer by dsamuelson · Aug 31, 2015 at 02:46 PM
fapawel · yesterday 1 Unlike Share You're first calling Destroy on the generator object and then You check it's tag (lines 21 and 26). to destroy the room just Call Destroy(currentRooms[index]) before removing it from list (line 33).
$$anonymous$$y new script looks like this
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class IRGenScript : $$anonymous$$onoBehaviour {
public GameObject[] availableRooms;
public List<GameObject> currentRooms;
public GameObject[] roomToRemove;
//public GameObject roomtoRemove;
private float roomWidth;
private int myLayer$$anonymous$$ask = 1 << 10;
private Vector3 positionRoom;
void Start ()
{
}
void FixedUpdate()
{
RaycastHit2D hitdown = Physics2D.Raycast ((GameObject.Find ("CreateNext").transform.position) , Vector2.down,$$anonymous$$athf.Infinity, myLayer$$anonymous$$ask);
if (hitdown.collider != null) {
positionRoom.y = hitdown.collider.transform.position.y;
roomWidth = transform.position.x;
}
if (hitdown.collider == null) {
int randomRoomIndex = Random.Range (0, availableRooms.Length);
GameObject room = (GameObject)Instantiate (availableRooms [randomRoomIndex]);
room.transform.position = new Vector2 (roomWidth, positionRoom.y + 1.9f);
currentRooms.Add (room);
if (currentRooms.Count.Equals (3)){
Destroy (currentRooms[0]);
currentRooms.RemoveAt (0);
}
}
}
}
Which is attached to an empty game object called create next