- Home /
Two objects with different damage - Not working?
I have two objects (triggers) that act as hazards they are identified by tags. "Hazards" do 25 damage while "Bullets" do 10, this is where my problem comes into play. Whenever I hit a hazard my player takes the damage of both tagged objects even though they only hit one. E.g. Player hits a Hazard (25) but they lose 35 health (Hazard + Bullet)
Any ideas? :/
Here's my code:
var fullHealth : int = 100;
var curHealth : int = 100;
var Ship : GameObject;
var DestroyedShip : GameObject;
var DeathCam : GameObject;
function OnTriggerEnter(collider : Collider) {
if(collider.tag == "Hazard");
curHealth -= 25;
print ("WHAM!");
if(collider.tag == "Bullet");
curHealth -= 10;
print ("PING!");
}
function Update () {
if(curHealth >= fullHealth){
curHealth = fullHealth;
}
if(curHealth <= 0){
curHealth = 0;
Debug.Log("KABOOM!");
Destroy(Ship);
Instantiate(DestroyedShip,transform.position, transform.rotation);
Instantiate(DeathCam,transform.position, transform.rotation);
}
}
function OnGUI() {
GUI.Label (Rect (25, 40, 100, 20), "Health = "+curHealth);
}
why don't you debug.log the collider.tag. maybe that will help you figure out the problem.
You might want to change it to:
function OnTriggerEnter(collider : Collider) {
if(collider.tag == "Hazard") {
curHealth -= 25;
print ("WHA$$anonymous$$!");
} else if(collider.tag == "Bullet") {
curHealth -= 10;
print ("PING!");
}
}
But yeah you should do a print to find out what the collider's tag and what the collider's GameObject is. We really need more information about these GameObject's that are tagged Hazard and Bullet. Do they have children? Are any of their children mistagged?
In order to avoid any such problems, I'd suggest always (and religiously) using {}
.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141406im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Damage to Health 2 Answers
Help with health reduction script 1 Answer
Collision, tags, and triggers 1 Answer
This collision Enemy health code suddenly stopped working. 1 Answer
Character controller mess up damage 3 Answers