I expect that the part you want to optimize is collision.collider.name.Equals("") because you used the Unity Profiler and found out that this particular line of code takes up an unacceptable amount of execution time (I would find that unlikely, though). You also might just want to find out if the other object is friend, enemy or neutral and consider the name of the collider an inappropriate method of doing so (Which I would consider a much better reason).
Let's also assume that you have more than two factions in your game. Because if you only have players and enemies, you don't need an enum to store faction membership. A bool would be much simpler.
In order to do that, you could add a component FactionMembership to each object in your game (or at least those where the friend/foe status is relevant). That class is a wrapper around an enum with all the factions an object can belong to:
public FactionMembership: MonoBehaviour {
public enum Faction {
CATS,
DOGS,
HAMSTERS
}
public Faction faction;
}
When you add this component to a GameObject, you will have a dropdown menu in its inspector where you can pick the faction it belongs to between "CATS", "DOGS" and "HAMSTERS".
You don't need to create a separate MonoBehaviour for this, by the way. You could just add this enum and public variable to an existing one if you have one where it makes sense. But I don't know what else you got in your project, so let's go with a dedicated behaviour for this.
Now back to your collision function. I assume you want to find out if the object belongs to a different faction. In that case you would do this:
private void OnCollisionEnter2D(Collision2D collision)
{
// get the faction membership components of both objects.
// note: if you call this function a lot, you might want to
// store myFactionMembership in a private class-level variable
// which you read in Start to improve performance
FactionMembership myFactionMembership = GetComponent<FactionMembership>();
FactionMembership otherFactionMembership = collision.gameObject.GetComponent<FactionMembership>();
// do both objects in this collision actually have a faction membership?
// If not, then we are not interested in this collision.
if(myFactionMembership == null || otherFactionMembership == null) {
return;
}
// check if we have encountered an enemy
if (myFactionMembership.faction != otherFactionMembership.faction) {
Console.log("Enemy encountered!");
}
}
This code of course assumes that all 3 factions we have in this game are mortal enemies who will always fight each other. But this might not always be the case. For example, what if the dogs and the hamsters have forged a temporary alliance against the cats? In that case it might make sense to add a helper method bool IsEnemyOf(FactionMembership other) to FactionMembership.
FactionMembership myFactionMembership = GetComponent<FactionMembership>();
FactionMembership otherFactionMembership = collision.gameObject.GetComponent<FactionMembership>();
if(myFactionMembership == null || otherFactionMembership == null) {
return;
}
if (myFactionMembership.IsEnemyOf(otherFactionMembership)) {
Console.log("Enemy encountered!");
}
How that method determines which factions are enemies and which are not is outside of the scope of this answer, because the logic for this could get quite complex depending on what exactly your requirements are.
enumfeature and another which doesn't but which I think is a better solution for your actual problem. Please pick the one you consider more helpful to you. \$\endgroup\$