Problem with raycasting and enemy health bars

Hi, I’m trying to make a script to show enemy health bars only if the player is aiming ,otherwise, hide health bars. Enemy health bars are nested as children to enemy objects in screen space coordinates.
I use a raycast for aiming.

this is my script for aiming:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class EmitRay : MonoBehaviour {
	public static RaycastHit hit;
	public static bool EnemyHit;
	public bool isHit;
	Ray ray;
	// Use this for initialization
	void Start () {

		EnemyHit = false;
		isHit = false;

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

	
		int x = Screen.width / 2;
		int y = Screen.height / 2;
		
		 ray = Camera.main.ScreenPointToRay(new Vector3(x, y,0));

		if (Physics.Raycast (ray, out hit, 950.0f)) {
			
			if(hit.collider.tag == "Enemy"){
				EnemyHit = true;
				isHit = true;
				hit.collider.SendMessage ("HitByRay");
			}else {
				EnemyHit = false;
				isHit = false;

			}
		} 


	}




}

and this is my script for enemy object:

using UnityEngine;
using System.Collections;

public class DetectHit : MonoBehaviour {

	public GameObject aimingManager;
	EmitRay emitray;
	// Use this for initialization
	void Start () {
		transform.GetChild (0).gameObject.SetActive(false);
		emitray = aimingManager.GetComponent<EmitRay>();
	
	}
	
	// Update is called once per frame
	void Update () {



	}

    void HitByRay () {
      

		if (emitray.isHit) {
			transform.GetChild (0).gameObject.SetActive (true);
		} else {
			transform.GetChild (0).gameObject.SetActive (false);
		}
	}
	
	


}

The problem with my script is when I aim at enemy, health bar shows up but never hides even if the variable " isHit" is false !!! I don’t know why.

Please could anyone help me?

in your code:

     if (Physics.Raycast (ray, out hit, 950.0f)) {
         
         if(hit.collider.tag == "Enemy"){
             EnemyHit = true;
             isHit = true;
             hit.collider.SendMessage ("HitByRay");
         }else {
             EnemyHit = false;
             isHit = false;

         }
     } 

you need ‘else’ for ‘if (Physics.Raycast (ray, out hit, 950.0f))’

This is what you wrote: if raycast hit: check if it is enemy, if it is not enemy (raycast still hit) then set hit vars as false. But you do not have any statement what to do if raycast does not hit anything. I would believe you need set hit vars as false over there.

I propose, deleting the DetectHit class, it’s not needed and make EmitRay keep track of which object that was last hit.

using UnityEngine;
using System.Collections;

public class EmitRay : MonoBehaviour
{
    private GameObject lastHitObject = null;

    // Update is called once per frame
    void Update()
    {
        int x = Screen.width / 2;
        int y = Screen.height / 2;

        Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y, 0));

        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 950.0f))
        {

            if (hit.collider.tag == "Enemy")
            {
                // if the hit object is not the same as the lastHitObject and lastHitObject is not null, then hide child in lastHitObject
                if (lastHitObject != hit.collider.gameObject)
                {
                    if (lastHitObject != null) lastHitObject.transform.GetChild(0).gameObject.SetActive(false);
                }

                // show the child on the hit object
                hit.transform.GetChild(0).gameObject.SetActive(true);
                // keep track of which object was last hit
                lastHitObject = hit.collider.gameObject;
            }
        }
        else// raycast hit no object
        {
            // if lastHitObject is present, then hide the child
            if (lastHitObject != null) lastHitObject.transform.GetChild(0).gameObject.SetActive(false);
            // reset lastHitObject to null
            lastHitObject = null;
        }
    }
  
}

Hey guys, thanks to all of you. Finally I found the answer !!

this is the aiming script:

using UnityEngine;
using System.Collections;


public class EmitRay : MonoBehaviour {
	public static RaycastHit hit;
	public static bool EnemyHit;
	public static bool isHit;
	Ray ray;
	int x = Screen.width / 2;
	int y = Screen.height / 2;

	GameObject lastHitObject = null;

	DetectHit detectHit;
	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		ray = Camera.main.ScreenPointToRay(new Vector3(x, y,0));
	
	    Physics.Raycast (ray, out hit,100.0f) ;

		detectHit = hit.collider.gameObject.GetComponent<DetectHit> ();

			if (hit.collider.tag == "Enemy") {
			lastHitObject= hit.collider.gameObject;
			EnemyHit = true;
			isHit = true;
			print ("hit");
			detectHit.HitEnemy();

		} else if(lastHitObject !=null){
			isHit = false;
			print ("not hit");
			DetectHit detectHit2;
			detectHit2=lastHitObject.GetComponent<DetectHit>();
			detectHit2.NotHit();

		}
	
	}

}

and this is the enemy script:

using UnityEngine;
using System.Collections;

public class DetectHit : MonoBehaviour {
	

	public GameObject healthbar;


	public void HitEnemy(){
		healthbar.SetActive (true);
	}

	public void NotHit(){
		healthbar.SetActive(false);
	}

}

Finally it works.