I feel like there is a better way to do this. Right now in each if condition I have to get the trigger, the gameobject, and ask for its component. I’m looking for a way to just get the component once, and ask if it equals this or this… It just seems inefficient to have to keep using GetComponent. As a programmer, when I see I have to do something over and over, it’s a heads up that something could be better.
Any ideas?
For example:
void OnTriggerEnter2D(Collider2D trigger){
var triggerHit = trigger.gameObject.GetComponent<T>();
if(triggerHit == LedgeTrigger){
...
}
...
}
// Actual Code
void OnTriggerEnter2D(Collider2D trigger){
if(trigger.gameObject.GetComponent<LedgeTrigger>() != null)
HitLedge();
else if(trigger.gameObject.GetComponent<WallTrigger>() != null){
HitWall();
}
else if(trigger.gameObject.GetComponent<Door>() != null){
HitDoor();
}
}