My character is not taking damage.
Hey guys, this is one of my first scripts in Unity, so I am pretty weak, but any help is much appreciated. Basically I want my character to take 20 points of damage every time he moves into a fire (there is a 2.5 second delay though if he's already in the fire). My issue is that when he goes into the fire, nothing happens. His current health remains at 100. I am using Unity 5.3.1f.
This is my code so far:
public class Fire_Damage : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public Slider HealthBar;
public float timeperBurn = 2.5f;
public int FlameDamage = 20;
private Player_Control player_Control;
private bool isDead;
GameObject player;
FireHealth FCHealth;
float timer;
bool onFire;
GameObject fire;
void Awake()
{
currentHealth = startingHealth;
player_Control = GetComponent<Player_Control>();
player = GameObject.FindGameObjectWithTag("Player");
FCHealth = GetComponent<FireHealth>();
fire = GameObject.FindGameObjectWithTag("Fire");
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject == fire)
{
onFire = true;
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject == fire)
{
onFire = false;
}
}
void Death()
{
isDead = true;
player_Control.enabled = false;
}
void Update()
{
if (onFire)
{
if (timer > Time.time)
{
currentHealth -= FlameDamage;
timer = Time.time + timeperBurn;
HealthBar.value = currentHealth;
if (currentHealth <= 0 && !isDead)
{
Death();
}
}
}
}
}
In the console I only get the error that FireHealth is never used (that's my next task). So what am I missing here? Thanks.
Answer by jgodfrey · Feb 21, 2016 at 03:37 PM
The only place where "timer" is assigned a value is inside an if block where you verify that "timer > Time.time". However, since "timer" is never given an initial value, I don't see how you could ever get inside that if block.
(Not able to work on it right now) So I should assign timer a value and then it'll work?
Your answer
