Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

88
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda