- Home /
Question by
KolyanKolyanitch · Apr 24, 2014 at 11:56 AM ·
gameobject.tag
CompareTag - NullReferenceException
Collider2D Obj = Physics2D.OverlapCircle(transform.position, 0.05F);
if(Obj.CompareTag("SomeTag"))
{
isActive = true;
}
NullReferenceException: Object reference not set to an instance of an object.
What am I doing wrong? How to fix it?
Comment
Best Answer
Answer by KiraSensei · Apr 24, 2014 at 11:58 AM
If Physics2D.OverlapCircle does not give you a collider, it's because there are no colliders in the circle, as said in the doc HERE.
So you need to check the value first :
Collider2D Obj = Physics2D.OverlapCircle(transform.position, 0.05F);
if(Obj && Obj.CompareTag("SomeTag"))
{
isActive = true;
}
Please accept the answer by ticking the check box on the left of the answer if it answered your question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143455im_/https://answers.unity.com/themes/thub/images/avi.jpg)