[Tips] What is the best way to deal with different enemy targeting?

Let me expand on this:

I’m creating a point and click RPG and I’ve reached an impass: The way I’m handling targeting right now is I’m only pulling 1 enemy from that enemies script and using it to get a target (I’ll show my code in a sec)

What I want to know is, what is the best way to get the target and pull it’s health, stats, etc…

Here is how I’m currently getting a singular target: [This is just how I’m finding the target, and displaying its health bar]

public Texture2D healthBar;
public Texture2D healthBarFrame;

private int healthBarWidth = 50;
private int healthBarHeight = 5;

private float left;
private float top;

private Vector3 healthBarScreenPosition;

public PlayerCombat player;
public Skeleton target;
public float healthPercent;
void Update () {
	if(player.opponent != null) {
		target = player.opponent.GetComponent<Skeleton>();
		healthPercent = (float)target.skeletonCurHealth / (float)target.skeletonMaxHealth;

		Vector3 healthBarWorldPosition = (target.transform.position + new Vector3(0.0f, target.transform.lossyScale.y, 0.0f));
		healthBarScreenPosition = Camera.main.WorldToScreenPoint(healthBarWorldPosition);
		left = healthBarScreenPosition.x - (healthBarWidth / 2);
		top = Screen.height - (healthBarScreenPosition.y + (healthBarHeight / 2));
	} else {
		target = null;
		healthPercent = 0;

void OnGUI() {
	if (target != null) {
		GUI.DrawTexture(new Rect(left, top, 50, 5), healthBarFrame);
		GUI.DrawTexture(new Rect(left, top, (50 * healthPercent), 5), healthBar);

But now lets say I have another enemy, a spider or something, how could I get it’s health from its own script?

I have messed around with this sort of method:

void getTarget() {
	if( Input.GetMouseButtonDown(0)) {
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		if(Physics.Raycast( ray, out hit, 100)) {

			if (hit.transform.gameObject.tag == "Environment") {
				player.GetComponent<PlayerCombat>().opponent = null;
				attack = false;
			} else {}

I guess what i’m asking is: What is the best way of handling this sort of targeting?

(sorry for the long post!)

You are on the right track.

What you are trying to do is called a “Pick ray”. if you google “Unity3D Pick ray” you will find lots of advice on it.

You can use layers to control what a pick ray will actually intersect with. You probably want a layer for everything that is targetable.