Hello,
i have made a health script that reduces the health when the object gets hit by a raycast. The script works for 1 gameibject per scene, but i want to be able to use it for multiple gameobjects (targets, enemies etc).
These are the scripts i have:
The raycastgun:
using UnityEngine;
using System.Collections;
public class GunHit
{
public float damage;
public RaycastHit raycastHit;
}
public class RaycastGun : MonoBehaviour
{
public float fireDelay = 0.1f;
public float damage;
public string buttonName = "Fire1";
public LayerMask layerMask = -1;
private bool readyToFire = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown (buttonName) && readyToFire)
{
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.forward,out hit, Mathf.Infinity,layerMask))
{
//Health verlagen wanneer
Health.currentHealth = Health.currentHealth - damage;
Debug.Log (Health.currentHealth);
}
}
}
}
and the health:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public float maxHealth = 100.0f;
static public float currentHealth = 100.0f;
// Use this for initialization
void Start ()
{
currentHealth = maxHealth;
}
void Update ()
{
if (currentHealth <= 0)
{
gameObject.SetActive(false);
}
}
}
thanks,