Something like OnTriggerExit for Raycasts?

Hello, I’m looking for a something like OnTriggerExit for the Raycast. I’ve made a script: when the Raycast touch the tagged object, his material change, but when the raycast leave it, the material stay… I want it to return to his anciant material :confused:

Some help?

Cordially.

I don’t work with raycasts because they’re taxing, but do you have an else statement following the if? IE:

if(raycasthit){
change color;
} else{
return color;
}

the process to return the color should be the same as when you applied it, so it shouldn’t be hard. I imagine it’s something like, “else if(raycast.hit == null)” or “else if(raycast.hit != this.gameObject” or something, but like I said, I don’t go near em.

void Hit_Target() // put this in update function

{

Ray ray = Cam.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit))
    {
        if (hit.collider.gameObject.GetComponent<Block_Piece>() != null) // If hit object has needed script (avoid highliting other objects)
        {
            if (Target != hit.collider.gameObject) // if I`m not already looking at it
            {
                if (Target != null) // if no object selected;
                {
                    Target.GetComponent<Block_Piece>().Hit(false); // Reverse the old one back in here
                }
                Target = hit.collider.gameObject; // make object my target
                Target.GetComponent<Block_Piece>().Hit(true); // call it`s script

            }
        }
        else if (Target != null) // 
        {
            Target.GetComponent<Block_Piece>().Hit(false); // Reverse the old one back in here
            Target = null;
        }
    }
    else if (Target != null) //
    {
        Target.GetComponent<Block_Piece>().Hit(false); // Reverse the old one back in here
        Target = null;
    }
}