Storing Renderer in a variable not working!

I have this in Update:

    		RaycastHit hit;
    		if (Physics.Raycast (transform.position, transform.forward, out hit, 80f) && !hit.transform.gameObject.CompareTag ("terrain")) {
    			Renderer rend = hit.transform.GetComponent<Renderer> ();
    			Renderer rendholder = hit.transform.GetComponent<Renderer> ();
    			rendholder.material.shader = hit.transform.gameObject.GetComponent<Renderer> ().material.shader;
    			rendeything = rendholder;
    			hit.transform.gameObject.GetComponent<Renderer> ().material.shader = Shader.Find ("Transparent/Diffuse");
    			Color tempColor = rend.material.color;
    			tempColor.a = 0.5f;
    			rend.material.color = Color.Lerp(rend.material.color, tempColor, 5f);
    			Debug.DrawLine (transform.position, hit.point);
    			hideything = hit.transform.gameObject;
    			Invoke ("Treset", 6f);

And this is its own function:

 	public void Treset(){
    		hideything.GetComponent<Renderer>().material.shader = rendeything.material.shader;
    		hideything.GetComponent<Renderer>().material.color = rendeything.material.color;
    		Debug.Log ("Reset mats on" + ());

The idea is, after 6 seconds, set the hideything’s shader and color back to what it was when the raycast function logged it, before changing it.

Honestly, I’m only trying to do it after a timer because I don’t know how to write "If the object is no longer being hit, return it to its original shader and color.

The debug log goes off, way too many times, but the objects are still transparent,
Would love if someone let me know what I’m doing wrong!

It is because when you call material.color, you edit the copy of that material, not the material itself. To deal with that, you have to either:

A) read the material to a temporary variable, edit the temporary material and then set it back. Be careful since it can be hard on memory if you change these a lot because it copies the material each time.

B) use sharedMaterial instead of material. This allows you to edit the actual material, not the copy. But be careful, since it edits that material and the changes persist even after stopping the game in the editor.

The compromise would be to duplicate the material using something like theRenderer.material = theRenderer.material in the start and then use sharedMaterial in your code. This would prevent from accidental edits in the editor and it will not be hard on memory.