Using the Tag System in Unity and Damaging the Player

Tags are a super helpful system in Unity that makes it easy for us to tell which objects are interacting with each other, and HOW they should interact within our code.They go hand-in-hand with Triggers, Colliders, and Rigidbodies. Here’s how they work!…


This content originally appeared on Level Up Coding - Medium and was authored by Frank Warman

Tags are a super helpful system in Unity that makes it easy for us to tell which objects are interacting with each other, and HOW they should interact within our code.

They go hand-in-hand with Triggers, Colliders, and Rigidbodies. Here’s how they work!

Enemies can now be properly destroyed!

The Tag System

Using the Tag system is simple and straightforward. It is giving an Object a tag, or name, to easily reference. Then when an Object interacts with ANY member of that tag classification, it performs an action with EVERY member in the same way.

To give an Object a tag, it is located underneath their Name in the Inspector window.

Some are premade, but you can create your own for any given circumstance. In our case, we’ll be creating one for our Laser.

Giving our Player a tag, and creating one for our Laser.

Now within our Enemy code, we can choose how it interacts with each tag during it’s collision detection, by using an if/else statement, and checking the tag — other.CompareTag(“Player”).

You could also use — other.tag == “Player” . They both work.

If the Enemy collides with our Player, destroy just the Enemy, but damage the Player (more on that later). If the Enemy comes in contact with a Laser, destroy both objects.

Setting up our tags in the Enemy Script.

Code in action:

Player Code Cleanup — Renaming private variables

Next we’ll be creating our Damage Player logic. But before that I want to clean up the Player code.

It’s best practice, and industry standard, to name private variables with an underscore “_” prefix. I still routinely mess that up, but it gives me a good chance to show you how to efficiently rename variables, and not screw up your whole code.

If you want to rename a variable, if you just change one reference, your code will break, since all your other instances of that variable are now incorrect.

The best way to do this is to select the variable you want to rename, make sure it gets highlighted, then press CTRL+R twice. This allows you to rename your variable and every instance of it. Super handy!

Another shortcut, that I’m using in the example above, is after pressing CTRL+R twice, I hit the Home key to go to the start of the variable and just input my underscore. Rather than retyping the whole variable name, as in the _canFire example.

Damaging The Player

Now back to our regularly scheduled programming.

To damage our player, we’ll have to do a couple things. Create a variable, create a public method, and then call that method from within our Enemy Script!

Creating our private int _lives. I made it a [SerializedField] for debugging purposes — to make sure it works properly. It’s also private so that no code can change it on us all willy-nilly. They will have to use our dedicated method.

Creating our _lives variable.

And our DamagePlayer() function:

Each time this is called, our _lives will decrease by 1. If _lives is equal or less than 0 (if we have a super strong enemy for instance that does more damage), then our Player is destroyed!

Now to call this function/method from another script! This requires some Script Communication. A big subject for another day, but we’ll be using the GetComponent<>() function to achieve this.

In our Enemy Script:

If the ‘other’ tag is Player, then we create a variable of type Player called player, which needs to get a reference to it’s Player script attached as a Component. (Hence GetComponent<Player>()).

The if (player != null) is a null check incase there is a bug that doesn’t accurately retrieve the component from the ‘other’ object.

Now that we have the variable player, that properly references it’s script component, we can call any function inside of the Player Script. In this case, it will be the DamagePlayer() that we just created!

Here is our full code in action:

You can see our lives decreasing by 1 every time we hit an Enemy!

Now we’re getting somewhere! There’s a consequence for being a bad pilot now!

Next up we’ll be spawning Enemies with a Spawn Manager, and have an Introduction to Coroutines!


Using the Tag System in Unity and Damaging the Player was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Frank Warman


Print Share Comment Cite Upload Translate Updates
APA

Frank Warman | Sciencx (2021-04-05T02:35:15+00:00) Using the Tag System in Unity and Damaging the Player. Retrieved from https://www.scien.cx/2021/04/05/using-the-tag-system-in-unity-and-damaging-the-player/

MLA
" » Using the Tag System in Unity and Damaging the Player." Frank Warman | Sciencx - Monday April 5, 2021, https://www.scien.cx/2021/04/05/using-the-tag-system-in-unity-and-damaging-the-player/
HARVARD
Frank Warman | Sciencx Monday April 5, 2021 » Using the Tag System in Unity and Damaging the Player., viewed ,<https://www.scien.cx/2021/04/05/using-the-tag-system-in-unity-and-damaging-the-player/>
VANCOUVER
Frank Warman | Sciencx - » Using the Tag System in Unity and Damaging the Player. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/05/using-the-tag-system-in-unity-and-damaging-the-player/
CHICAGO
" » Using the Tag System in Unity and Damaging the Player." Frank Warman | Sciencx - Accessed . https://www.scien.cx/2021/04/05/using-the-tag-system-in-unity-and-damaging-the-player/
IEEE
" » Using the Tag System in Unity and Damaging the Player." Frank Warman | Sciencx [Online]. Available: https://www.scien.cx/2021/04/05/using-the-tag-system-in-unity-and-damaging-the-player/. [Accessed: ]
rf:citation
» Using the Tag System in Unity and Damaging the Player | Frank Warman | Sciencx | https://www.scien.cx/2021/04/05/using-the-tag-system-in-unity-and-damaging-the-player/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.