Decreasing health on (raycast)hit

Hello,

a few weeks ago i made a shooting script that uses a raycast for shooting. Currently it spawn a particle prefab on hit but i want it to decrease the health of that object (if it has the tag “Enemy”)

These are the scripts i have:

shooting:

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 = 1.0f;
	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))
			{
				GunHit gunHit = new GunHit();
				gunHit.damage = damage;
				gunHit.raycastHit = hit;
				hit.collider.SendMessage("Damage",gunHit, SendMessageOptions.DontRequireReceiver);
			}
		}
	}
}

and a simple health script:

using UnityEngine;
using System.Collections;

public class Health : MonoBehaviour {

	public float maxHealth = 100f;
	public float currentHealth = 100f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

The fireDelay doesnt do anything yet but i am still working on that.

Thanks,
me

That is pretty easy, first make sure to set currentHealth as a static variable, with this you can access it from within another script. We will need to access this later on.
The method is (ScriptName.variable)

using UnityEngine;
    using System.Collections;
    
    public class Health : MonoBehaviour {
    
    	// Use this for initialization
    	public float maxHealth = 100.0f;
    	static public float currentHealth = 100.0f;
    
    	void Start () {
    	
    	}
    	
    }

When the player got hit you put this in the statement. This will change the value of currentHealth to a new value.

//Health.currentHealth will be the currenthealth left minus the damage you received
     Health.currentHealth = Health.currentHealth-damage;
        		Debug.Log (Health.currentHealth);