ArgumentNullException When using Collider2D.OverlapCollider()
The exact error popping up on my unity console is: ArgumentNullException: Value cannot be null. Parameter name: collider The exact line the error is on is "col.OverlapCollider(nonPlayer, colliders);" Which is extremely confusing to me. Also for further information the layer mask NPC is properly defined in earlier code and I have the seem problems even when i keep the contactFilter not having any specific parameters. On top of this the same error occurs even when i set col.OverlapCollider(nonPlayer, colliders); equal to an integer. as in: int h = col.OverlapCollider(nonPlayer, colliders);
public void Attack(Collider2D col, float stagger) //deals with the physical attack of the weapon and it's interaction with Characters {
ContactFilter2D nonPlayer = new ContactFilter2D()
{
};
nonPlayer.SetLayerMask(NPC);
nonPlayer.useLayerMask = true;
List<Collider2D> colliders = new List<Collider2D>();
col.OverlapCollider(nonPlayer, colliders);
Debug.Log("hits : " + colliders.Count);
for(int i = 0; i < colliders.Count; i++)
{
colliders[i].gameObject.GetComponent<Character>().ReceiveDamage(usedDmgTypes, usedDmgValues, stagger, player.is_FacingRight);
Debug.Log("we made it?");
}
}
If this is also helpful the error also gave out this information: UnityEngine.PhysicsScene2D.OverlapColliderList_Internal (UnityEngine.Collider2D collider, UnityEngine.ContactFilter2D contactFilter, System.Collections.Generic.List`1[T] results) (at :0) UnityEngine.PhysicsScene2D.OverlapCollider (UnityEngine.Collider2D collider, UnityEngine.ContactFilter2D contactFilter, System.Collections.Generic.List`1[T] results) (at :0) UnityEngine.Collider2D.OverlapCollider (UnityEngine.ContactFilter2D contactFilter, System.Collections.Generic.List`1[T] results) (at :0) Weapon.Attack (UnityEngine.Collider2D col, System.Single stagger) (at Assets/Scripts/GeneralParents/Weapon.cs:395)
Are you sure col
is actually set? How do you set its value?
Sorry it's taken so long to get back to you. And you are correct about where the error is co$$anonymous$$g from I've been looking in the wrong place this whole time but the new issue still baffles me. Here's the code:
for (int i = 0; i < normAtkCols.Count; i++) { if(normAtkCols[i] != null) { Debug.Log("InLocal"); }
normalAttackColliders.Add(normAtkCols[i]);
if(normalAttackColliders[i] != null)
{
Debug.Log("InGlobal");
}
}
So I ran a Debug to see how for it would make it and The "In Local" gets ran three times, the length of the List. But InGlobal doesn't get ran once. also normalAttackColliders is a List of public colliders defined by this scripts parent script.
Okay I found the solution instead of doing any of this that was reformatted from a different way I was acquiring the colliders to be put into normalAttackColliders List. I simply create my own local list of colliders then pass it into it like so: } normalAttackColliders = normAtkCols;
for (int i = 0; i < normAtkCols.Count; i++)
{
if(normalAttackColliders[0] != null)
{
Debug.Log("Success");
}
}
And here Success gets ran 3 times. Thank you so much for your help Hellium
Answer by balonieface · Aug 18, 2020 at 02:24 AM
The problem was that the Collider I was trying to call the Overlap collider function from was null. Was simple looking in the wrong place for the error.
Your answer
Follow this Question
Related Questions
C# unexpected symbol error 1 Answer
3D top down player rotation error 2 Answers
How to fix error CS0103 Cannot implicitly covert type 'bool' to 'string' 1 Answer
How To how ?? 2 Answers
Collision with object not working. 2 Answers