Detecting other objects on same layer with same tag without detecting it self?

Hello, I’m a newbie here, would be happy for some advice!
I’m trying to make an enemy object to detect other objects colliders with similar properties (make a prefab out of it)
for some reason in the last if statement when I write && gameObject != this.gameObject, it’s not working well, it’s not detecting itself, but also won’t detect other game objects with the same tag and layer.

public class BoxCast : MonoBehaviour
{
    private bool isDetecting;
    private bool Detection;
    private float CheckRadius = 2f;

    private void Update()
    {
        DetectionOther();
    }

    private void OnDrawGizmos()
    {
        if(isDetecting == true)
        {
            Gizmos.color = Color.red;
        }
        else
        {
            Gizmos.color = Color.yellow;
        }
        Gizmos.DrawWireSphere(transform.position, CheckRadius);
    }
        private void DetectionOther()
    {
       
        int mask = LayerMask.GetMask("AlienLayer");
        Detection = Physics2D.OverlapCircle(transform.position, CheckRadius, mask);

        if(Detection == true && gameObject.CompareTag("alien") && gameObject != this.gameObject)
        {
            isDetecting = true;
            Debug.Log("detecting");
        }
        else
        {
            isDetecting = false;
        }
    }
}

It seems you’re new to the C# language too beause “gameObject” is the same as" this.gameObject" in the same way as you could use “this.isDetecting” or “this.CheckRadius”.

You should also read the docs, especially when new: Unity - Scripting API: Physics2D.OverlapCircle

This call returns a Collider2D (the collider that overlaps) and not a bool (yes/no).

It seems this is more like what you want:

var hitCollider = Physics2D.OverlapCircle(transform.position, CheckRadius, mask);
if (hitCollider &
    hitCollider.gameObject.CompareTag("alien") &&
    hitCollider.gameObject != gameObject
{
}

The thing is, if this GameObject is on the layer then it’ll detect it but the above call only detects one so you won’t detect others overlapping. You should use the overload which allows you to pass a List for multiple results (see docs). Being new, I think you’ll need to go away an experiment with C# and learn some new stuff.

the code you wrote works like a charm! thank you so much for the explanations.
and yes im currently studying c# and unity, only about 2 months in, so i got a lot to learn and understand