Multiple Health Bar

So what I want to do is only show the enemies GUIBox (healthbar) when it’s clicked. Then if another enemy is clicked it switches to that one.

playerTarget:

public var playerTarget : Transform;
function Update()
    {
        //check if the left mouse has been pressed down this frame
        if (Input.GetMouseButtonUp(0))
        {
            //empty RaycastHit object which raycast puts the hit details into
            var hit : RaycastHit;
            //ray shooting out of the camera from where the mouse is
            var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
            if (Physics.Raycast(ray, hit))
            {
     
            if (hit.collider.gameObject.CompareTag("Enemy"))
            {
            //print out the name if the raycast hits something
     
            Debug.Log(hit.collider.name);
     
            playerTarget = hit.collider.transform;
     
            Debug.Log("PlayerTarget Successfully changed");
     
            //var pa : PlayerAttack = GetComponent("PlayerAttack");
     
            //pa.target = playerTarget;
     
            }
            else
            {
            Debug.Log(hit.collider.name + " no tag");
            }
     
          }
     
       }
     
    }

HealthMob:

using UnityEngine;
using System.Collections;

public class healthMob2: MonoBehaviour {
    public int maxHealth = 100;
    public int curHealth = 100;
    public float healthBarLength;
    // Use this for initialization
    void Start () {
    healthBarLength = Screen.width / 2;
    }

    // Update is called once per frame
    void Update () {
    AdjustCurrentHealth(0);
    }  

void die() {
        Destroy(gameObject);
    }   

void OnGUI()
 {
        GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth+ "/" + maxHealth);
        GUI.Label(new Rect((Screen.width) + 10, 40, healthBarLength, 20), "Allie");
    }
    public void AdjustCurrentHealth(int adj) {
        curHealth += adj;

        if(curHealth < 0)
        curHealth = 0;
        if(curHealth == 0)
        die();

        if(curHealth > maxHealth)
            curHealth = maxHealth;
        if(maxHealth < 1)
            maxHealth = 1;

        healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
    }
}

Can’t you just create a boolean on the enemy to tell if it’s selected or not ?

if it’s selected just show the GUI.Box like :

if (selected)
{
   GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth+ "/" + maxHealth);
}

You can make this boolean public and call it from another script.

And you can also use : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseOver.html to enable or disable your boolean.