Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in '2D' started by nicolascolaleo1, Jun 23, 2022.

  1. nicolascolaleo1

    nicolascolaleo1

    Joined:
    May 22, 2022
    Posts:
    6
    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.

    Code (CSharp):
    1. public class BoxCast : MonoBehaviour
    2. {
    3.     private bool isDetecting;
    4.     private bool Detection;
    5.     private float CheckRadius = 2f;
    6.  
    7.     private void Update()
    8.     {
    9.         DetectionOther();
    10.     }
    11.  
    12.     private void OnDrawGizmos()
    13.     {
    14.         if(isDetecting == true)
    15.         {
    16.             Gizmos.color = Color.red;
    17.         }
    18.         else
    19.         {
    20.             Gizmos.color = Color.yellow;
    21.         }
    22.         Gizmos.DrawWireSphere(transform.position, CheckRadius);
    23.     }
    24.         private void DetectionOther()
    25.     {
    26.        
    27.         int mask = LayerMask.GetMask("AlienLayer");
    28.         Detection = Physics2D.OverlapCircle(transform.position, CheckRadius, mask);
    29.  
    30.         if(Detection == true && gameObject.CompareTag("alien") && gameObject != this.gameObject)
    31.         {
    32.             isDetecting = true;
    33.             Debug.Log("detecting");
    34.         }
    35.         else
    36.         {
    37.             isDetecting = false;
    38.         }
    39.     }
    40. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    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: https://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html

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

    It seems this is more like what you want:
    Code (CSharp):
    1. var hitCollider = Physics2D.OverlapCircle(transform.position, CheckRadius, mask);
    2. if (hitCollider &
    3.     hitCollider.gameObject.CompareTag("alien") &&
    4.     hitCollider.gameObject != gameObject
    5. {
    6. }
    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<Collider2D> for multiple results (see docs). Being new, I think you'll need to go away an experiment with C# and learn some new stuff.
     
  3. nicolascolaleo1

    nicolascolaleo1

    Joined:
    May 22, 2022
    Posts:
    6
    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