- Home /
Remove object from List after gameObject was destroyed in between Scene loads
Hello! I have been working on a script for mye enemies a while back, which ended up working quite well. Now I have an issue with it, after implementing more scenes and loading these scenes. The scenario when things go wrong:
An enemy is standing within my character's triggerCollider, which makes the enemy appear in my list of enemies. But, when my character also stands inside a portal (stage change event) and I press a button to load a new scene, I will get an error with the list that the enemy in the list can not be found.
So, what I need is a code that can check if my colliderList contains (enemy) and if this enemy was deactivated or destroyed, and remove the enemy from the list accordingly. I've been trying things for myself, but ended up empty. So I hope you can help me putting me in the right direction of the answer!
Here is some code to understand how my code is set up:
List<Collider2D> colliderList = new List<Collider2D> ();
[in Update:]
foreach (Collider2D enemy in colliderList) {
EnemyHealth eh = (EnemyHealth)enemy.GetComponent ("EnemyHealth");
if(eh.curhp <= 0) {colliderList.Remove (enemy); return;}
//So, here I want something like: if(colliderList.Contains(enemy)
&& isNotHereAnymore) {colliderList.Remove(enemy);
}
//Trigger events for adding an enemy into the collider for MeleeAttack
void OnTriggerEnter2D (Collider2D col) {
if (!colliderList.Contains (col) && col.gameObject.tag == "Enemy") {
colliderList.Add (col);
}
}
void OnTriggerExit2D (Collider2D col) {
if (colliderList.Contains (col) && col.gameObject.tag == "Enemy") {
colliderList.Remove (col);
}
}
So, I can understand why it malfunctions, the enemy isn't removed in any way that I have stated that it should be removed, either by its hp being 0, or it exiting the triggerCollider. This is where I shout help. haha!
I hope some of you can bear with me on my ineptitude, cheers.
Answer by Baste · Dec 15, 2014 at 12:07 AM
You can't call any methods on the enemies from the last scene, or check if they're dead - they're not there any more.
You probably don't need any information from the last scene when you change levels - in that case, just ditch the old stuff in the list of colliders:
void OnLevelWasLoaded(int level) {
colliderList.Clear();
}
Then you have a nice list that's ready for the next level. If you need to keep anything in the list between scene loads, you'll have to remove all empty elements. If this only happens when you load a scene, you put this in OnLevelWasLoaded, if not, you can do it in update.
colliderList.RemoveAll(item => item == null);
That'll remove ever item in the list that are null, which is what's causing the problem. If you haven't seen the syntax before, look up C# lambdas.
I didn't know about the .Clear function. This was pretty nice. So, what I did was just adding this to a coroutine that returns 0, (meaning 1 frame)
IEnumerator waitFrame() {
yield return 0;
this.gameObject.SetActive(false);
}
This way I have one frame of the object not seeing any enemy before he is setInactive, and there is time to clear the list right before he turns inactive. And when he turns active again, the character is working fine :) Thanks for your help, it helped shed light on the issue! Cheers!
Thanks for the clear and easy-to-use answer! Worked perfectly.