Health and Damage Script Not Working
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.
I know the problem line is this one:
health--;
but I don't know how to fix it. Please explain to me in detail because I'm an idiot. Thanks!
Sorry for sending it twice but I still haven't received any answer that works. 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 liqvidunity · Feb 17, 2016 at 05:30 AM
In your code Enemy.gameObject.SendMessage("OnDamage", damage);
you are passing damage value to OnDamage method.But you are not receiving that value. Kindly update method OnDamage -
void OnDamage(float damageVal){
health = health - damageVal;
if (health <= 0) {
Destroy(gameObject);
}
}
When I save this code I receive 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?)
Answer by Zoogyburger · Feb 17, 2016 at 12:58 AM
Not exacly sure about your problem but this worked for me: make sure the player is called player an change your MoveBullet script to this:
public class MoveBullet : MonoBehaviour {
public int damageToGive;
void OnCollisionEnter (Collision other)
{
if (other.gameObject.name == "Player")
{
other.gameObject.GetComponent<PlayerHealthManager> ().HurtPlayer(damageToGive);
}
}
}
You can now change the damage it gives inside the Inspecter Change your Damage script to this:
public int PlayerMaxHealth;
public int PlayerCurrentHealth;
// Use this for initialization
void Start () {
PlayerCurrentHealth = PlayerMaxHealth;
}
// Update is called once per frame
void Update () {
if(PlayerCurrentHealth <=0)
{
gameObject.SetActive (false);
}
}
public void HurtPlayer(int damageToGive)
{
PlayerCurrentHealth -= damageToGive;
}
public void SetMaxHeatlh()
{
PlayerCurrentHealth = PlayerMaxHealth;
}
}