The Importance of Tags!

Objective: Understanding and implementing tags.

Tag is a reference word that you can assign to GameObjects. They help you identify GameObjects for scripting purposes. Tags are useful for triggers in collider control scripts. They need to work out whether the player is interacting with an enemy, a prop, or a collectable for example. You can use the GameObject.FindWithTag() function to find a GameObject by setting it to look for any object that contains the Tag you want.

Setting up collisions if you have a powerup set to activate OnTriggerEnter2D like the example below.

Technically works!

The powerups will work as intended. If they collide with the player then the function will be called and the powerup will be destroyed afterwards. However, anything else with a collider will also destroy this object, meaning if an enemy or laser were to collide with the powerup the powerup will be destroyed.

If you want only the player to interact with the powerup then you can use Tags.

Now the only collision that will activate the powerup will be the object with the Tag “Player”.

Now we can control what objects will trigger collisions with Tags, you can use Tags for other functions as well, don’t be afraid to experiment and I’ll see you in the next tutorial.