Performance: Tags and layers
Tags and layers affect how Unity filters and processes game objects during rendering and physics calculations, impacting frame rate and responsiveness.
if (((1 << other.gameObject.layer) & enemyLayerMask) != 0) { /* process collision */ }
if (other.gameObject.tag == "Enemy") { /* process collision */ }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using string tags for filtering | N/A | N/A | N/A | [X] Bad |
| Using layer masks for filtering | N/A | N/A | N/A | [OK] Good |
Tags in Unity?gameObject.tag == "TagName" to check tags.layer is an int, not string; HasTag and CheckTag are not Unity methods.void Start() {
if (gameObject.layer == 8) {
Debug.Log("Layer is 8");
} else {
Debug.Log("Other layer");
}
}layer property is an integer representing the object's layer number.gameObject.layer == 8 is true, so it prints "Layer is 8".void Update() {
if (gameObject.tag = "Player") {
Debug.Log("Player detected");
}
}= which assigns "Player" to gameObject.tag instead of comparing.== operator, so it should be gameObject.tag == "Player".Pass the test and this pattern moves unknown