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.
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) {
// ...
}
}