Best practice for OnTriggerEnter detection

For best performance, use tag, layer, or a GameObject reference?

void OnTriggerEnter (Collider other) {

  1. if (other.gameObject.tag == “Player”)
  2. if (other.gameObject.layer == LayerMask.NameToLayer (“Player”))
  3. if (other.gameObject == MyController.instance.gameObject)

I assume the tag should be the fastest right?

Hi.

Firstly, when comparing tags you should use gameObject.CompareTag as it’s more efficient (I forget the details). The bonus of using layers is that you can prevent the physics simulation even running the checks on objects you don’t want to collide with each other in the first place by using the physics matrix. I think it does depend on the implementation, but we setup the matrix to only trigger collisions we want/expect. We then use Tag or even check for expected components.

I hope that helps =D