Skip to main content
Discovery icon for Official Unity Discord Discord server

Official Unity Discord

32,909 Online
140,022 Members
# ๐Ÿ’ปโ”ƒcode-beginner5 people

replacing tags with GetComponent

Avoid magic-string tags by querying components directly with TryGetComponent to identify entities and invoke public methods.

  • Unity tags act as magic strings that are prone to bugs and best avoided when identifying game objects
  • Calling a method on a retrieved script requires storing the component reference from GetComponent rather than calling the method on the GameObject
  • TryGetComponent replaces tag comparisons by checking for the target script and assigning an out variable in a single step
  • Methods called from external scripts must be marked public to be accessible across components
Summarized from recent messages, might be inaccurate

More conversations

Talk, Play, &
Hang Out

Find your space to hang out, discuss the latest updates with the party chat, and see who's down to join your next match.
Get App