Object collision interaction

I’m curious what methods people use for having classes interact with each other when they collide. For example if a projectile collides with something that can take damage.

I’ve always done something like this:

private void OnCollisionEnter(Collision other) {
    var damageable = other.gameObject.GetComponent<IDamageable>();
    damageable?.Damage(_damage);
}

It works just fine, but I’d prefer not to use GetComponent. Any more clever solutions?

GetComponent is probably the most common way to do this. (However, I wasn’t aware that GetComponent worked with interfaces, as you have in your example…) It’s the approach I use for things like bullet fire.

Another common (but less recommended) approach is to use tags. But even if you do that, you’d probably still end up needing to call GetComponent() to get a script to interact with.