How can i health to my enemies?

This is my enemy script

public class Enemy : MonoBehaviour 
{
	public float Range = 10;
	public float Speed = 1;
	GameObject Player; 
	public static int EnemiesKilled;
		// Use this for initialization

		void Start () 
	{
		Player = GameObject.Find("PLAYER");
	}
	
	// Update is called once per frame
	void Update () 
	{
		float distance = Vector3.Distance(transform.position,Player.transform.position);
		
		if (distance < Range)
		{
			FollowPlayer();
			
			if(distance < 2)
			{
				Player.GetComponent<Player>().force += (Player.transform.position - transform.position).normalized * 0.2f;
				Player.GetComponent<Player>().health--;
			}
		}
	}
	
	void FollowPlayer()
	{
		transform.LookAt(Player.transform);
		transform.Translate(Vector3.forward * Time.deltaTime * Speed);
	}
	
	void OnDestroy()
	{
						EnemiesKilled+=10;
	}

		}

and this is my bullet script

public class BulletCS : MonoBehaviour 
{
	private GunCS	gun;		// reference to a gun that fired this bullet.
	
	
	public float 	maxLifetime = 5.0f;
	private float	lifetime;
	
	
	// --------------------
	public void Init(GunCS gun)
	{
		this.gun = gun;
		this.lifetime = 0;
	}
	
	
	// ---------------
	void FixedUpdate()
	{
		// Destroy this bullet if it didn't hit anything...
		
		if ((this.lifetime += Time.deltaTime) > this.maxLifetime)
			Destroy(this.gameObject);
	}
	
	
	// ------------------
	
	void OnTriggerEnter(Collider otherObject)
	{
		if(otherObject.name == "untitled")
		{
			
			Destroy(otherObject.gameObject);
			
		}
		if(otherObject.name == "Enemy")
		{
			Destroy(otherObject.gameObject);
		}
	}
}

help please :frowning:

I don’t know whether or not you instantiate your enemies or have them in an array, or have another method of enemy generation. Therefore, the first thing I will tell you is how to instantiate your enemies, so that a health script can be applied to them.

  1. Have a prefab that will be your enemy in the game world.

  2. Customize this template script which instantiates the enemy into the world.

    var thePrefab : GameObject;

    function Start ()
    {
    var instance : GameObject = Instantiate(thePrefab, /Position/, /Rotation/);
    }

I’m sorry about that bit, you see, the coding button wouldn’t do its job despite multiple tries.

Or, you could paint trees and, in those positions, your enemies will be instantiated!

var treeFab : Transform;

//The selected terrain is a certain terrain
var grabTerrain : Terrain;
//The trees are in an instance
var trees : TreeInstance[];
 
function Start ()
{
	Instantiate();
}

function Instantiate ()
{
	//For when i is worth zero, i gets the tree instances data on the terrains and their lengths
	for (var i=0; i <Terrain.activeTerrain.terrainData.treeInstances.Length; i++)
    {
        var terrain : TerrainData  = Terrain.activeTerrain.terrainData;
		
		var pos = Vector3.Scale(Terrain.activeTerrain.terrainData.treeInstances*.position,terrain.size)+ Terrain.activeTerrain.transform.position;*
  •  Instantiate(treeFab, pos, Quaternion.identity);*
    

}
}
Now that that’s the instantiation, we can get on with the coding.
Scripts for an instantiated object are to be applied to its original prefab.
Player Health Tutorial: Unity 3D Tutorial Part 18: Giving Our Character Health - YouTube
Health Specific to Enemies: Unity 3D Tutorial Part 16: Enemy Health - YouTube
Now, you can’t just destroy it any more, so please add this function to your health script.
function OnCollisionEnter ()
{
if (col.gameObject.name == “Bullet”)
{
/Variable for Enemy’s Health/ -= /However much you want to take away on a hit/;
}
}
I am assuming that the bullet is called ‘Bullet’. I have left the other variable places for you to fill in.