I am relatively new to unity, and still learning the ropes. I am trying to make a script to update health on collision (with a dagger, sword, etc.)
I have 2 problems.
1)the dagger I give to my first person controller is not colliding with objects
2)when 2 things collide, the message to run the “hit” method is being lost.
here is my code:
Health script
using UnityEngine;
using System.Collections;
[System.Serializable]
public class HealthManager : MonoBehaviour {
public int maxHP;
public int curHP;
void Start(){
curHP = maxHP;
}
void Hit(int dmg){
curHP -= dmg;
if(curHP <= 0)
Death();
}
void Death(){
if(curHP > 0)
curHP = 0;
Transform.Destroy(this);
}
void Heal(int hp){
curHP += hp;
if(curHP > maxHP)
curHP = maxHP;
}
}
damage script
using UnityEngine;
using System.Collections;
[System.Serializable]
public class WeaponHandler : MonoBehaviour {
public int dmg;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(collider.transform.tag == "Enemy")
collider.transform.SendMessage("Hit", dmg,SendMessageOptions.DontRequireReceiver);
else if(collider.transform.tag == "Vital")
collider.transform.SendMessage("Death", SendMessageOptions.DontRequireReceiver);
else if(collider.transform.tag == "Armor")
collider.transform.SendMessage("Damage", dmg/2,SendMessageOptions.DontRequireReceiver);
}
}