Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
Differentiate Gameobject colliders with the same tag

In my OnTriggerEnter2D() I have a if condition which checks if the right collider is colliding with my other Gameobject. For a simple Example: There is a Lion and a Elephant with the Tag "Animal" with the same script that manages their variables. Based on which of the two Animals enter the other Collider I want to execute specific code. I tried to give both animals a string which tells which type they are as in Lion is a Lion. But that didnt work.

public void OnTriggerEnter2D(Collider2D a_collider2D)
{

    if (a_collider2D.gameObject.CompareTag("Animal") && m_sAnimalStat.m_sAnimalType.Equals("Lion")) 
    {//code here if its a Lion}

Is there another way to tell colliders apart? Putting them on different Layers didnt work out either. OnTriggerEnter2D is on the other Gameobject where the animals are walking towards. The AnimaStatManager is on the Lion Prefab which has the Tag "Animal". As said it manages various variables for all my animals. Like the m_sAnimalType. m_aAnimalStat=GameObject.FindGameObjectWithTag("Animal").GetComponent(); --> just so i can access what i need from that script.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Unity objects are defined by composition. This means an object is defined by it's components. So, in my opinion, a better approach could be having a "Lion" component on a Lion object, except you have a specific reason not to.

You take your class Animal : MonoBehaviour and create a child class for each species: class Lion : Animal, class Elephant : Animal, etc.

Then, you can simply do:

public void OnTriggerEnter2D(Collider2D a_collider2D)
{
    if (a_collider2D.GetComponent<Lion>() != null) {
        // Lion
    }
    else if (a_collider2D.GetComponent<Elephant>() != null) {
        // Elephant
    }
    else if (a_collider2D.GetComponent<...>() != null) {
        // ...
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!