Switching from object to object with Raycast Problem

Hello everyone reading this.

I hope someone knows how to crack this!

I use a Raycast that makes my enemies(asteroids) HealthUI pop up.

When my players Raycast deselects the asteroid, my UI disables which is perfect.

BUT: When I go select an asteroid after another asteroid (with no space in between) the UI of the previous asteroid stays on screen. I tried a lot of things. But obviously just not the right thing.

Here’s my script:

public Transform brickPlayer;
	public LineRenderer lineRenderer;
	public LayerMask mask;
	private AsteroidUI1 AsteroidUI;				
	public Transform NewHitObject;


	void FixedUpdate()
	{
		Ray ray = new Ray (transform.position, transform.forward);
		RaycastHit hitInfo;

		if (Physics.Raycast (ray, out hitInfo, mask)) 
		{
			Debug.DrawLine (ray.origin, hitInfo.point, Color.red);
			NewHitObject = hitInfo.collider.transform;

			if (hitInfo.collider.tag == "Asteroid") 
			{ 
				lineRenderer.SetPosition (0, brickPlayer.position);
				lineRenderer.SetPosition (1, hitInfo.point);
				lineRenderer.enabled = true;

				ShowAsteroidUI ();
			} 
		} 
		else
		{
			if (NewHitObject == true) 
			{
				StopShowAsteroidUI ();
			}

			Debug.DrawLine (ray.origin, ray.origin + ray.direction * 100, Color.green);
			lineRenderer.enabled = false;
		}
	}


	public void ShowAsteroidUI()
	{
		NewHitObject.GetComponent<AsteroidUI1> ().ShowUI ();
	}

	public void StopShowAsteroidUI()
	{
		NewHitObject.GetComponent<AsteroidUI1> ().StopShowUI ();
	}

Hopefully this is what you’re after. I essentially added a local variable to store the “old” hit object before things get going, then used that variable to first stop the old hit object’s UI in the case where you find a new hit object.

 	public Transform brickPlayer;
     public LineRenderer lineRenderer;
     public LayerMask mask;
     private AsteroidUI1 AsteroidUI;                
     public Transform NewHitObject;
 
 
     void FixedUpdate()
     {
         Ray ray = new Ray (transform.position, transform.forward);
         RaycastHit hitInfo;

		 // store the hit object in a local variable
		 var oldHitObject = NewHitObject;
 
         if (Physics.Raycast (ray, out hitInfo, mask)) 
         {
             Debug.DrawLine (ray.origin, hitInfo.point, Color.red);
             NewHitObject = hitInfo.collider.transform;
 
             if (hitInfo.collider.tag == "Asteroid") 
             { 
                 lineRenderer.SetPosition(0, brickPlayer.position);
                 lineRenderer.SetPosition(1, hitInfo.point);
                 lineRenderer.enabled = true;
 
				 // hide the old hit object UI before showing the new one
                 StopShowAsteroidUI(oldHitObject);
                 ShowAsteroidUI(NewHitObject);
             } 
         } 
         else
         {
             if (oldHitObject == true) 
             {
                 // clear the hit object just so this code doesn't execute over and over
                 NewHitObject = null;
                 StopShowAsteroidUI(oldHitObject);
             }
 
             Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.green);
             lineRenderer.enabled = false;
         }
	 }
 
 
     public static void ShowAsteroidUI(Transform t)
     {
         t.GetComponent<AsteroidUI1>().ShowUI();
     }
 
     public static void StopShowAsteroidUI(Transform t)
     {
         t.GetComponent<AsteroidUI1>().StopShowUI();
     }