- Home /
[Solved] Enemy Won't Affect Player Health?
So I have this script attatched to an invisible cube with a box collider that is a trigger attatched to my player...
public var healthamt : float;
var tex1 : Texture;
private var attacked : boolean;
function OnTriggerEnter(Col : Collider){
if (Col.gameObject.tag == "enemy"){
attacked = true;
}
}
function Update(){
if (attacked){
healthamt -= Time.deltaTime;
}
if (healthamt > 100){
healthamt = 100;
}
}
function OnGUI(){
GUI.color = new Color32(255, 255, 255, 100 - healthamt);
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), tex1);
}
But when my enemy enters the collider it doesn't change my healthamt variable at all.
Why, god? Why?
First step is to check if your OnTriggerEnter is firing, but putting the following immediately after line 5.
Debug.Log("Trigger hit a :" + Col.gameObject.tag);
Some notes on OnTriggerXXX:
Are your enemies tagged correctly (capitalisation is important).
At least one collider must have a rigidbody attached for OnTriggerXXX to fire.
Two triggers will not fire if they intersect.
Okay, I'll try adding the rigid body first, because I don't think it had it on. Afterwards I will see if there are 2 triggers. Next time I get to it, but I'm sure this answered my question. Thank you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Hurting by Gravity force HELP!!! ? 2 Answers
Player Health and Death 1 Answer
Problem with enemy health. 1 Answer
Enemy healthbar script 2 Answers