- Home /
How to null-reference check a Collider2D that's been destroyed
Hello! I have been playing around with a twinstick control system for my game, and it's beginning to come up roses! But, there is now an issue with checking the collider when the collider (or gameObject) has been destroyed. To understand visually: My player has a CircleCollider2D that will attack any enemy colliding with the trigger when he is directing the right stick towards them.
My question is simply: How do I null reference check a Collider2D so the method can keep going as normal?
My current code:
List<Collider2D> colliderList = new List<Collider2D>();
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);
}
}
void OnTriggerStay2D(Collider2D col) {
float addedDamageRange = Random.Range(2,6);
float damageDealt = attackDamage + addedDamageRange;
float rightHorizontal = Input.GetAxis("RightStickHorizontal");
float rightVertical = Input.GetAxis("RightStickVertical");
if ((rightHorizontal != 0 || rightVertical != 0) && meleeAttackCD == 0f) {
foreach(Collider2D enemy in colliderList) {
EnemyHealth eh = (EnemyHealth)enemy.GetComponent("EnemyHealth");
if(enemy.collider2D) {
eh.curhp -= damageDealt;
}
Debug.Log ("You hit enemy for "+damageDealt+" dmg");
meleeAttackCD = 1f;
}
}
}
When this code executed enough so that an enemy has been destroyed (his curhp == 0), it stops working and just gives me a null reference error looking for the previous BoxCollider2D on the enemy. I hope you can bare with me and understand. Any help is appreciated.
Where exactly you get a null reference error? Can you post the exact error message from console?
Sure thing, have this wall of mess!
$$anonymous$$issingReferenceException: The object of type 'BoxCollider2D' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Component.get_gameObject () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineComponent.cs:172)
UnityEngine.Component.GetComponent (System.String type) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineComponent.cs:192)
TwinStickRotation.OnTriggerStay2D (UnityEngine.Collider2D col) (at Assets/Scripts/TwinStick/TwinStickRotation.cs:82)
Answer by HarshadK · Nov 06, 2014 at 11:58 AM
In your OnTriggerEnter you add the collider to your colliderList which is traversed in your OnTriggerEnter. But I guess you are actually destroying your enemy in your EnemyHealth script when it reaches to zero but you are not removing his collider from the colliderList.
Just add a check in side your health decrease for if the health is zero and then remove that collider from the colliderList.
Try:
if(enemy.collider2D)
{
eh.curhp -= damageDealt;
if(eh.curhp <= 0)
{
// Remove this enemy's collider from the list since he is dead
colliderList.Remove(col);
}
}
It worked, sorta. It left me with this error, though:
InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.Collections.Generic.List`1+Enumerator[UnityEngine.Collider2D].VerifyState () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:778)
System.Collections.Generic.List`1+Enumerator[UnityEngine.Collider2D].$$anonymous$$oveNext () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:784)
TwinStickRotation.OnTriggerStay2D (UnityEngine.Collider2D col) (at Assets/Scripts/TwinStick/TwinStickRotation.cs:81)
How do I fix this then, doctor? :) Thanks btw!
you welcome @S$$anonymous$$lthygolem I accidentally deleted it although.