Is there a way for an object to tell if it's being hit by a raycast?

As stated, there are many answers here that show how to get a raycast to return that it has hit an object. I was wondering if there was a way to do it backwards. Object says, yes I have been hit by a raycast, now I’m not.

Maybe create a bool called IsBeingHit in the respective object’s script… Then use raycasts to just toggle it between true and false (By referencing the script with RaycastHit)


Note : In Update() add 2 if statements, one for when IsBeingHit is true and one for when IsBeingHit is false.

It would be better to create a function that you would use instead of raycast, which raycasts and also sends a message to any object it hits with a script. It could be like this:

bool Raycast(Ray ray, out RaycastHit hitInfo)
{
     bool hit = Physics.Raycast(ray, hitInfo);

     if (hit)
     {
          hit.collider.gameObject.SendMessage("OnRaycastHit", gameObject);
     }

     return hit;
}

And on any monobehaviours you would like to know that it was hit by a raycast, you add the method: void OnRaycastHit(GameObject hitter)

@armorhide406