- Home /
How to destroy game objects from a list
Hi all,
Please note my code is in C#
I have a question on destroying game objects. Ok I'll try to be as descriptive as possible. I have a script attached to my player that spawns enemies while the player is active. I have a collider set up so when my player collides with an enemy, the enemy is destroyed. My problem is when I destroy the enemy it disappears but still exists within the game. (doesn't fully destroy) If I call destroy to the actual game object (i.e. on the script attached to my enemy, if I say): if (health <=0) Destroy(gameObject); - all current instances of the enemy are destroyed, and not just the one I hit. I'm wondering what the best way to go about this is. Someone told me to track each enemy in a list and destroy each enemy at the stored place in the list. I.e. (psudocode) foreach (enemy i in list) destroy enemy (i); - but I can't find a way to make this code work. Here is how I add my enemies to my list as well as the code behind my spawn function:
public class MyClass : MonoBehaviour
{
public GameObject enemyPrefab;
public int maxShips = 1;
List testEnemy;
//EnemyScript is the script attached to my enemy. It makes it move, stores health for the enemy, etc.
EnemyScript enemyScript = new EnemyScript();
void Start ()
{
testEnemy = new List();
}
//I have a timer that fires off every so often and I call this when the timer ticks - I don't include the timer code here.
public void SpawnEnemy()
{
for (int i = 0; i < maxShips; i++)
{
testEnemy.Add((GameObject)Instantiate(enemyPrefab, new Vector3(boundsRight.x, (Random.Range(boundsTop.y - 1.0f, boundsBottom.y + 0.5f)), 7), Quaternion.identity));
}
}
//Here is my collider
void OnTriggerEnter(Collider otherObject)
{
if (otherObject.tag == "Enemy")
{
Destroy(this.enemyScript.gameObject);
testEnemy.Remove(enemyScript.gameObject);
}
}
}
so if I collide with an enemy, the enemy disappears - but isn't actually destroyed. This is an issue because I'm trying to track how many enemies the player was not able to destroy. If an enemy makes it past the screen, I increment this amount by 1. So if I hit the enemy it disappears and to the player it looks as if it was destroyed, but it is still in motion and will eventually pass off the screen (because technically it still exists) - thus skewing my results. Can anyone give me a hand?
Answer by hiddenspring81 · May 28, 2013 at 12:15 AM
Try this instead. I haven't tested this code, so let me know if there are any problems.
public class MyClass : MonoBehaviour
{
public GameObject enemyPrefab;
public int maxShips = 1;
List<GameObject> enemies;
void Start ()
{
enemies = new List<GameObject>();
}
public void SpawnEnemy()
{
for (int i = 0; i < maxShips; i++)
{
enemies.Add((GameObject)Instantiate(enemyPrefab, new Vector3(boundsRight.x, (Random.Range(boundsTop.y - 1.0f, boundsBottom.y + 0.5f)), 7), Quaternion.identity));
}
}
void OnTriggerEnter(Collider otherObject)
{
if (otherObject.tag == "Enemy")
{
Destroy(otherObject);
enemies.Remove(otherObject.gameObject);
}
}
}
Answer by dcolwell634 · Jul 20, 2013 at 04:27 AM
thanks @hiddenspring81 for your answer. And sorry for the late response. This is what I was looking to do at the time, BUT I found out an easier was to accomplish it. I just had the enemy ships destroy themselves when they were hit. Then if they pass the screen, it means I didn't shoot them and increment missed integer. I think it didn't work because when I called the destroy game object from this script, instead of the enemy script, it was just destroying the first gameobject in the enemy ship queue and not all objects. So it still had a transform.
Answer by Taomujw · Jan 22, 2016 at 01:39 PM
you can make a function with destroy(gameObject) and sendmessage to the object through the list to destroy it.