Turning off vars when rays stop hitting, & Help with code structure: If in update or While statement

First project, first big roadblock:

So I have units of different colors that all constantly shoot lasers unless they are getting damaged by enemies. While unit A shoots unit B, unit B’s laser changes color. Once unit B is no longer getting shot it changes back to its original color.

Everything works fine except for when unit A’s ray is no longer hitting unit B. (If I turn them off manually in the editor it works)

I can’t turn off the hittingPlayer variable of the unit shooting the laser because there is no way to get out of the else if once I make it true. Maybe there is a better way(hittingPlayer is just there so the laser colors only get added once)

I also can’t turn off the variable on the unit being hit because I was using the Raycast hit to turn it on.

Right now I have this all in Update()
I feel like I need to split some of it up and have like a while(hittingPlayer) loop going in a coroutine or something like that to make it work. Other ways I have thought about solving it have me shooting twice as many rays which I don’t want to do.

if(damaged == false)  //If you aren't taking damage shoot your laser

        {
            line.enabled = true;
            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit;
         
            line.SetPosition(0, ray.origin);
         
            if (Physics.Raycast (ray, out hit, 30))
            {
                line.SetPosition(1, hit.point);


                if(hit.collider.tag == "Player" && hittingPlayer == false)
                {

                    hit.transform.GetComponentInChildren<Laser>().gettingHit = true;
                    hit.transform.GetComponentInChildren<Laser>().tempShipColor = tempShipColor + hit.transform.GetComponentInChildren<Laser>().tempShipColor;
                    hittingPlayer = true;
                     
                }
                else if(hit.collider.tag == "Player" && hittingPlayer == true)
                {
                    Debug.Log ("Yay!");

                }
         
                else if(hit.collider.tag == "Enemy")
                {
                    Debug.Log("Ouch!");
                }

                else     //never gets here because of the previous statements
                {
                    hittingPlayer = false;
                    hit.transform.GetComponentInChildren<Laser>().gettingHit = false;
                    Debug.Log("It Works!");
                }
            }
            else
                line.SetPosition(1, ray.GetPoint(30));
             
                hit.transform.GetComponentInChildren<Laser>().gettingHit = false;
               //this causes an error since it is not hitting it anymore, but I feel like if I had something that would do
              //the equivalent here it would work. Is this the kind of thing you use SendMessage for?


        }
        else
        line.enabled = false;

So really I’m just not sure how to turn something off when it isn’t being hit by the raycast anymore and would just like general advice on how to structure this.

Use a bool to record if a player has been hit. Check the bool to see if the player has been hit. Reset the bool at the end of the frame.

You probably want a manager script to control this. Consider using events for each ‘tick’ you need.