About 'Using Tags In Unity'!

In this video (objectives)…

  1. Getting schwifty with tags - understanding the what and why of using them.
  2. Creating an unbreakable block type which... doesn't... break!

After watching (learning outcomes)… Able to use tags to create an unbreakable block type.

(Unique Video Reference: 29_BR_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

It would be nice if there was an editor option to have tags be enums instead of strings.

Hello Rick, i have a little question, instead using tag Breakable and Unbreakable, why we not making 2 kind of prefab object, which is Breakable Blocks and Unbreakable Blocks?

also what is difference between

if (tag == “tag name”) and if(param.gameObject.tag == “tag name”)?

which i found that using if (tag == “tag name”) doesn’t work for me?

that its thanks!

Rick,

One minor improvement for the strings used in tags. They should be defined as constant strings at the top of the class. This prevents accidentally mistyping a string and the string is not duplicated through the code. Same as with “magic” numbers being defined as a constant value assigned to a variable.

So I put this in my code:
const string breakable = “Breakable”; // Tag value for a breakable block
const string unbreakable = “Unbreakable”; // Tag value for an unbreakable block

So now my code in Start() to count the breakable clocks looks like this:
if (CompareTag(breakable))
{ // Only count the “Breakable” blocks
currentLevel.CountBlocks();
} // if

And the code for collisions looks like this:
private void OnCollisionEnter2D(Collision2D collision)
{
if (CompareTag(breakable))
{
HandleHit();
} // if
else if (CompareTag(unbreakable))
{
// Reserved for future use :slight_smile:
} // else

}   // OnCollisionEnter2D()

Privacy & Terms