- Home /
Best practice for OnTriggerEnter detection
For best performance, use tag, layer, or a GameObject reference?
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Player")
if (other.gameObject.layer == LayerMask.NameToLayer ("Player"))
if (other.gameObject == MyController.instance.gameObject)
I assume the tag should be the fastest right?
Answer by Chris_Dlala · Sep 26, 2014 at 10:42 PM
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
Your answer
Follow this Question
Related Questions
Detect when a trigger has entered a collider 1 Answer
Collision Detection When Picking Up GameObjects 1 Answer
Trigger Spawning? 1 Answer
Colliding two GameObjects 1 Answer