- Home /
Question by
julinan3 · Mar 02, 2019 at 10:34 AM ·
unity 5not workinghealth
Health change than object health is not decreasing ?
Hi,I'm creating a survival shooter game.I have 3 script target,gun and score board.I'm trying to do score = 10 change health 60 or + 10 but changed moment object is health is not decreasing,pls help.
public float damage = 5f;
public float range = 100f;
public float fireRate = 15f;
public Camera fpsCam;
public ParticleSystem Muzzle;
public void Button_Click ()
{
if (Input.GetButton("Fire1"))
{
Shoot();
}
}
public void OnPointerDown()
{
InvokeRepeating("Shoot", 0, 0.2f);
}
public void OnPointerUp()
{
CancelInvoke("Shoot");
}
void Shoot ()
{
Muzzle.GetComponent<ParticleSystem>().Play();
RaycastHit hit;
if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if(target != null)
{
target.TakeDamage(damage);
}
}
}
and
public float health;
public void Update()
{
if (ScoreScript.scoreValue == 10)
{
health = 60f;
}
}
public void TakeDamage (float amount)
{
health -= amount;
if(health <= 0f)
{
ScoreScript.scoreValue += 10;
Die();
}
}
void Die()
{
Destroy(gameObject);
}
}
Comment
Best Answer
Answer by Tsaras · Mar 02, 2019 at 01:14 PM
public void Update()
{
if (ScoreScript.scoreValue == 10)
{
health = 60f;
}
}
It seems to me that this is your problem. You keep setting health = 60 in every frame. You need to have a way to turn this off once it fires, if I understood your problem correctly.
Answer by WewserLethalDude · Mar 02, 2019 at 12:28 PM
The void Button_Click() is never called in the script, and hence does nothing. You have to call it in Update() for the Input.GetButtonDown to be checked on every frame.
I changed it according to what you said but my problem still continues :(
Your answer
![](https://koobas.hobune.stream/wayback/20220612192758im_/https://answers.unity.com/themes/thub/images/avi.jpg)