How can i chose the previous target

I want to chose a target by Ray, and change it’s color to red. And when Im not pointing on the same object ( target) or choseing another target, i want it to turn back to it’s original color. How can i make that?

In the Update() function you will likely cast the Ray and determine what GameObject the ray struck. If it struck a game object, record the game object that was struck in a variable as well as recording its current material color. Now change the color of the GameObject to be what you want it to be.

In the Update() function, when the Ray strikes an object (see above), also compare to whether it was the SAME object as previously detected (currentGO == savedGO) or … if the ray didn’t strike any object, then using the previous savedGO (if one exists), set its color to be the previously saved color.

The easy is to use unity built-in rays on colliders. In your case, OnMouseEnter would change the color, OnMouseExit would reset it.