Question by
grdomenico · Feb 10, 2018 at 11:54 PM ·
bulletdamagehealthbarhealth
Bullet Damage Player's Healthbar,Bullet Damage Other Player's Health
Hi,I setted the bullet script that when i shot a player his healthbar take 10% damage,but when i shoot a player his healthbar go to 10%. Bullet Script:
void OnCollisionEnter(Collision collision)
{
GameObject hit=collision.gameObject;
Health health=hit.GetComponent<Health>();
if (health != null)
{
health.TakeDamage(10);
}
Destroy(gameObject);
}
Health Script:
public const int maxHealth = 100;
public int currentHealth = maxHealth;
public RectTransform healthbar;
public void TakeDamage(int amount)
{
currentHealth = amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("Dead");
}
healthbar.sizeDelta = new Vector2(currentHealth * 2, healthbar.sizeDelta.y);
}
}
Sorry for my bad English, Wrasty
Comment
Best Answer
Answer by RavenHardt · Feb 11, 2018 at 12:01 AM
@grdomenico Health Script:
public const int maxHealth = 100;
public int currentHealth = maxHealth;
public RectTransform healthbar;
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("Dead");
}
healthbar.sizeDelta = new Vector2(currentHealth * 2, healthbar.sizeDelta.y);
}
}
This is the fixed version, you forgot to take away the amount instead you were setting your health to it!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220612152359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Health and Damage Script Not Working 2 Answers
Huge Health and Damage Script Problem 0 Answers
Why my enemy health bar script is not working? 1 Answer
Health bar/damage script not working. Please help. 0 Answers
Kill Count and Health Bar Scripts 0 Answers