How to make health and damage script function correctly?
Hello everyone,
I'm making a third person shooter. For the shooting I'm using a bullet prefab. I have found some scripts that would apply well to my game but they seem to be malfunctioning. I have this script for the damage, which I have attached to the characters and the player
using UnityEngine;
using System.Collections;
public class Damage : MonoBehaviour {
public int health = 100;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnDamage(){
health--;
if (health <= 0) {
Destroy(gameObject);
}
}
}
And I also have this script for the bullet prefab.
using UnityEngine;
using System.Collections;
public class MoveBullet : MonoBehaviour {
public float damage = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider Enemy){
if(Enemy.gameObject.CompareTag("enemy"))
{
Enemy.gameObject.SendMessage("OnDamage", damage);
}
}
}
So, the health of the characters is 100. And each hit should be 5 points of damage. However, when I hit play, each bullet only gives me 1 point of damage. Strangely, when I fire a bullet, my character's health also drops by one. So, if I'm hit health drops by one, if I fire health drops by one again.
I actually like the character's health dropping by one when I fire, but why doesn't the bullet give me the specified amount of damage? Which is 5 not 1. I can't understand what I've done wrong with these scrips and why they aren't working properly. Please help! Thanks!
Answer by ata_2 · Feb 17, 2016 at 11:38 AM
hi move this line from MoveBullet code to Damage
public float damage = 5.0f;
and make this line change in Damage code from
health--;
to
health-=damage;
note: up code like
health= health - damage;
if you want you can write it like this
I have followed your instructions but I get the following error: Assets/Scripts/Damage.cs(16,17): error CS0266: Cannot implicitly convert type
float' to int'. An explicit conversion exists (are you missing a cast?)
Please help me out with this. Thanks!
Answer by TreyH · Feb 16, 2016 at 01:17 AM
void OnDamage(){
health--;
if (health <= 0) {
Destroy(gameObject);
}
}
This function just reduces your health by 1, per the 'health--;' line. Small oversight. :-)
Thanks for the reply. It's really helpful. The problem is that if I remove that problematic line, I get parsing errors and the code doesn't work. If I change it to this: void OnDamage (){ health = -damage; if (health <= 0.0) { Destroy(gameObject); } Then I get the error
Assets/Scripts/Damage.cs(16,27): error CS0103: The name `damage' does not exist in the current context
If I change it this: void OnDamage(float damage){ health = -damage; if (health <= 0.0) { Destroy(gameObject); } I get the error:
Assets/Scripts/Damage.cs(16,17): error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)
And if I change
public int health = 100; to
public float health = 100.0f;
Then the game suddenly stop when the first bullet hits me, as if the character died. Although the bullet only carries 5 damage. I get no errors in this instance. How should I proceed to make it work? Also, thanks for the help and sorry for being a noob.
Answer by Drfox80 · Jul 13, 2020 at 02:59 PM
also, Change in damage to a public void OnDamage(){
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class damge : $$anonymous$$onoBehaviour {
public int health = 100;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnDamage(int damge)
{
health -= damge;
if (health <= 0)
{
Destroy(gameObject);
}
}
}
public class $$anonymous$$oveBullet : $$anonymous$$onoBehaviour {
public float damage = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider Enemy){
if(Enemy.gameObject.CompareTag("enemy"))
{
Enemy.gameObject.Send$$anonymous$$essage("OnDamage", damage);
}
}
}