Outscal Logooutscal logo

Tags & Layers

Download

Unity Cheat Sheet: #41

Working with Tags

  • Assign tags directly: gameObject.tag = "Player"
  • Check tags efficiently: CompareTag() instead of == (avoids garbage)
  • Find single objects: FindWithTag() (use only during setup)
  • Find multiple objects: FindGameObjectsWithTag() returns array

Layer-Based Physics

  • Access layers safely: LayerMask.NameToLayer("LayerName")
  • Ignore collisions: Physics.IgnoreLayerCollision(layer1, layer2, true)
  • Check collision states: GetIgnoreLayerCollision(layer1, layer2)
  • Remember: Available layers are 0-31 only

Camera Rendering Control

  • Include layers: Camera.cullingMask = LayerMask.GetMask("Default", "UI")
  • Exclude layers: Camera.cullingMask &= LayerMask.GetMask("UI")
  • Combine multiple layers with commas in GetMask()
  • Note: Culling affects rendering only, not physics!

Advanced Layer Queries

  • Create layer masks: LayerMask.GetMask() or (1 << layerNumber)
  • Find objects by layer: Physics.OverlapSphere(position, radius, layerMask)
  • Filter results with layerMask parameter
  • Use OverlapSphereNonAlloc() to prevent garbage collection

Pro Tips:

  • Tags for identification, layers for interaction/rendering rules
  • Cache results from Find methods in Awake()/Start()
  • Set IgnoreLayerCollision at startup, not every frame
  • Define layer constants in a static class for consistency

Show More