- Home /
C# Check if Collider2D[] Doesn't contain Scripted GameObject
Is there a way to check if a Collider2D[] doesn't contain the gameobject it's attached to and/or exclude it? I have a scripted gameobject with a collider and Physics2D.OverlapCircleAll returns with that collider when I only want it to return only the colliders around the gameobject.
     public Collider2D[] hitColliders;
     
     void  Update (){
        hitColliders = Physics2D.OverlapCircleAll(transform.position, 5f);
     }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by HarshadK · Feb 18, 2015 at 06:27 AM
You can loop through the hitColliders array to find out the gameobject and then exclude it.
Something like:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ExcludeScript : MonoBehaviour {
 
     public List<Collider2D> hitColliders;
      
     void  Update (){
         Collider2D[] hitResult = Physics2D.OverlapCircleAll(transform.position, 5f);
             hitColliders = new List<Colider2D>();
         for(int i = 0; i < hitResult.Length; i++)
         {
             // if the transform of this collider's GO is not same as this GO's transform then it add it to the list
             if(hitResult[i].transform != this.transform)
             {
                 hitColliders.Add(hitResult[i]);
             }
         }
         //Once done you can even convert this list to an array
         hitColliders.ToArray();
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
How to prevent collider overlap during prefab replacement 0 Answers
Make Ray hit own collider 1 Answer
How to check is object directly next to another object? 1 Answer
Distribute terrain in zones 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                