- Home /
Healthscript not working
My health script is not working and i can't find the solution. The problem is that if I hit the enemy (a cube for example) with the bullet he won't die/disappear.
This is my health script
pragma strict
var Health : int = 100;
var bullet : GameObject;
var damage : int = 25;
function Update ()
{
if(Health < 0)
{
DIE();
}
}
function OnTriggerEnter (hit : Collider)
{
if(hit.gameObject.tag == "Bullet")
{
bullet = hit.gameObject;
TakeDamage ();
}
}
function TakeDamage ()
{
var GunPreferences : Bullet = bullet.GetComponent("Bullet");
damage = GunPreferences.Damage;
Health -= damage;
}
function DIE ()
{
Debug.Log("I'm dead");
Destroy(gameObject);
}
My Gun Script
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag: float = 30;
var IsFullAuto = true;
static var AmmoLeft : float;
static var ReloadTTime : float;
private var CanFire = true;
var FireRate = 0.1;
function Start () {
AmmoLeft = AmmoInMag;
ReloadTTime = ReloadTime;
}
function Update () {
if(IsFullAuto == false){
if(Input.GetButtonDown("Fire1")){
if(AmmoLeft > 0){
BroadcastMessage("FireAnim");
Fire();
}
}
}
else{
if(Input.GetButton("Fire1")){
if(AmmoLeft > 0){
BroadcastMessage("FireAnim");
Fire();
}
}
}
if(AmmoLeft == 0)
{
Reload();
}
if(AmmoLeft < 0){
AmmoLeft = 0;
}
}
function Fire(){
if(CanFire == true){
var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet1.AddForce(bullet1.transform.forward *BulletSpeed);
CanFire = false;
yield WaitForSeconds(FireRate);
CanFire = true;
AmmoLeft -= 1;
audio.Play();
}
}
function Reload(){
CanFire = false;
BroadcastMessage("ReloadAnim");
yield WaitForSeconds(ReloadTime);
CanFire = true;
}
Thanks in advance!!
Put a debug log in that prints Health every time it drops. Chances are thats never happening.
Put a debug in at the top of the OnTriggerEnter to see if thats getting called.
Then you will have a lot more information to debug from.
This is how you debug code. You start by checking all your assumptions til you find one that isn't true, then you ask "why isn't his true?"
Answer by rdb · May 22, 2014 at 03:41 PM
There are so many factors that could cause this not to work, so without seeing how you have things set up in the editor it's impossible to narrow down. I will point you at some areas I would look at first:
Line 13 you are checking OnTriggerEnter
, so make sure you set your collider to being a trigger.
Line 15 you check if hit.gameObject.tag == "Bullet"
, so make sure you actually have your bullet prefab tagged as "Bullet".
Line 25 you assign the value of GunPreferences.Damage
to be what you decrease health by, so make sure that is initialized in the Bullet
class and not defaulted to 0
.
And on a side note, line 7 you probably want to check if(Health <= 0)
instead of just <
.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can I change this script to use GUITexture instead of GUI number, for health display? 1 Answer
Damage/Healing over time 1 Answer
How do I modify my script to give my gun recoil when I shoot? 1 Answer
how do i access an int var in a javascript and -= it from another javascript 1 Answer