Checking for collisions without using strings

I’ve read somewhere that it’s a good habit to only use strings for displaying actual text in Unity. I’ve just watched a dozen tutorials about interfaces and was hoping to try and detect collisions more elegantly and reduce dependencies on tags or gameObject names. However, the complexity involved in checking collisions between objects with varying behaviors is starting to become a bit too much for me. As an example, I managed to make the following work on an Entity base class from which many of my objects inherit (the player, enemies, powerups, obstacles…)

    public virtual void OnCollisionEnter2D(Collision2D collision)
        IKillable killableEntity = gameObject.GetComponent<IKillable>();
        IPlayerWeapon playerWeapon = collision.gameObject.GetComponent<IPlayerWeapon>();
        if (killableEntity != null && playerWeapon != null)

This kills an entity if it’s killable and if it collides with a player projectile weapon. However, if I try to do this with every object in the game it’s going to turn into a big mess. Is there a more elegant way to check for collisions (either using interfaces or something else), knowing that some collisions need to trigger a completely different behavior (e.g. ICollectable) ?


Honestly, you may not like this answer, but currently there really isn’t anything better than using tags.
Yeah I know, sucks, but running a GetComponent like you are right now is worse than running 200 tag checks at once (Figuratively speaking).

The only thing I can really say is, try to create a new type of system that gets rid of tags.
That’s one thing I’ve always loved about programming, you can always make things better, it’s just finding the way to do it.

But using GetComponent isn’t the way, that’s taking 20 steps back.