- Home /
Can you change a variable based on the tag of an object?
Hey. I have a pretty basic question. This is not a problem i have tested because i don't have access currently to Unity, but i would like to see if it is possible to use the following code.
I have three enemy prefabs: "Unit1, "Unit2" and "Unit3". I want the code to know which unit it is so that it gives that unit a certain number of starting health points (i.e. 1 health point for unit1, 2 for unit2 and 3 for unit3).
I know how to assign each unit with health, but is the use of '(self.tag == "Unit1")' going to actually work?
Here is the code and thank you in advance: var self : GameObject; //self is the enemy prefab that this code has been placed on var healthPoints: int = 0; function Start(){ if (self.tag == "Unit1"){ healthPoints = 1; } if (self.tag == "Unit2"){ healthPoints = 2; } if (self.tag == "Unit3"){ healthPoints = 3; } } btw I'm not sure if the code sample thing is working. If not, can somebody fix it?, because in the edittor it looks fine
Answer by Vonni · Jan 04, 2013 at 04:54 PM
Self might work, but I'm pretty sure gameobject.tag would work if not.
thanks Vonni i realised i have something that has that in my collisions: if (collision.gameObject.tag == "Waypoint5"){ //if the unit reaches the last waypoint Destroy(self); } so it is "GameObject.gameObject.tag"
Your answer
![](https://koobas.hobune.stream/wayback/20220613090353im_/https://answers.unity.com/themes/thub/images/avi.jpg)