Enemy health only to appear when target is clicked on.

Hey everyone I have been working on an rpg and have run into a problem :( I honestly have no idea how to word this but i'll do my best. Basically when I click on a target and it has a tag "Enemy" then the objectis put into a var/transform in my targeting script which is coded in JS. At the same time it's also put into a transform called target in a c# script called player attack (Not sure if i'm saying this right i'm new to unity.)

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.

I have the targeting and attack scripts everything working perfectly. I can click to target and attack different enemies and kill them the only thing is is that I don't know how to make it so that the healthbars show up. The healthbars them selves are coded and are there it's just I don't know how to switch them.

If anyone could help me that would be amazing! I'll post my targeting script and enemyhealth script if that will help below. This is the only thing stopping me from progressing as what is the point in carrying on if I can't target an enemy :D

TARGET SCRIPT

targetjs.js

    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");
        }

      }

   }

}

ENEMY HEALTH SCRIPT - Attached to all mobs

EnemyHealth.cs

using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {
    public int maxHealth = 100;
    public int curHealth = 100;

    public AudioClip death; 

    public float healthBarLength;
    // Use this for initialization
    void Start () {
    healthBarLength = Screen.width / 2;
    }

    // Update is called once per frame
    void Update () {
    AdjustCurrentHealth(0);
    }
void dropLoot() {
         audio.PlayOneShot(death);
        Debug.Log("death should have played?");
    }   

void PlayAudioClipDeath() {

    }

void die() {

        dropLoot();
        Destroy(gameObject);
    }   

void OnGUI(){
        GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth+ "/" + maxHealth);
                GUI.Label(new Rect((Screen.width / 2) + 10, 40, healthBarLength, 20), "Enemy");

    }
    public void AdjustCurrentHealth(int adj) {
        curHealth += adj;

        if(curHealth < 0)
        curHealth = 0;
        if(curHealth == 0)
        animation.Play("enemydie");

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

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

If anyone can help please do. I will have no problem with crediting you for you work.

Thanks, Mike

Unless you want to show multiple health bars at the same time, consider having one health bar (object/script) and SendMessage to it with the currently-clicked enemy's info.

fixed it myself

Okay, There are no comments though.

This is my Targeting script in JS

public var playerTarget : Transform;

    public var HealthEnabled:boolean = false;

    public var lastPlayerTarget;

    function ChangePlayerAttackTarget() {

    //      var pa : PlayerAttack = GetComponent("PlayerAttack");

    //      pa.target = hit.collider.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");

            if(HealthEnabled == false){

                    ShowHealth();

            }

            else

            {

                    HideHealth();

                    HealthEnabled = false;

            }

            var pa : PlayerAttack = GetComponent("PlayerAttack");

            pa.target = playerTarget;

        }

            else{

            Debug.Log(hit.collider.name + " - " + hit.collider.tag);

            }

          }

       }

    }

        function ShowHealth() {

            playerTarget.GetComponent("EnemyHealth").enabled = true;

            if(lastPlayerTarget != null)lastPlayerTarget.GetComponent("EnemyHealth").enabled = false;

            lastPlayerTarget = playerTarget;

    }

    function HideHealth() {

    playerTarget.GetComponent("EnemyHealth").enabled = false;

    }

this is my EnemyHealth Script this is a c# script so you have to put it outside the standard assets folder for it to work.

    using UnityEngine;
using System.Collections;

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

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

void PlayAudioClipDeath() {
    audio.PlayOneShot(death);
    }

void die() {

        dropLoot();
        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)
        animation.Play("enemydie");

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

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

please post you own fix so others like me can learn from it