Hello everyone!
Please help me!
I’m trying to fix a problem I don’t understand.
I’m performing a Raycast on an asteroid far away, which calls a UI health-bar above an asteroid (and more). But when the asteroid is destroyed (or even sometimes when the Raycast just leaves the object), the UI sometimes (!!!) stays hoovering over screen. I tried a lot of things, though I seem to not know WHY and HOW to fix this.
Here’s my code (+ some additional info in the comments):
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);
theHitObject = hitInfo.collider.gameObject;
if (hitInfo.collider.tag == "Asteroid") {
lineRenderer.SetPosition (0, brickPlayer.position);
lineRenderer.SetPosition (1, hitInfo.point);
lineRenderer.enabled = true;
hitInfo.collider.gameObject.GetComponent<AsteroidUI1> ().ShowUI ();
}
} else {
if (theHitObject != null) {
theHitObject.GetComponent<AsteroidUI1> ().StopShowUI ();
}
else if (theHitObject == null) {
return;
}
Debug.DrawLine (ray.origin, ray.origin + ray.direction * 100, Color.green);
lineRenderer.enabled = false;
theHitObject = null;
}
}