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" + hideything.name.ToString ());
}
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!
Answer by AurimasBlazulionis · Dec 20, 2016 at 10:20 AM
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.
TYS$$anonymous$$, Love answers that $$anonymous$$ch me things!
The thing though, isn't that what I am doing with Rendholder in my script? Im then sending Rendholder to Treset through rendeything, but it's still not working, as far as changing the material back.
I'm a little confused at this: theRenderer.material = the theRenderer.material, is that the syntax for it?
oh no, that is not the syntax, I made a mistake.
Your answer
Follow this Question
Related Questions
material color not changing 1 Answer
2 same Scripts w/ GetComponent : one works, one doesn't 2 Answers
How to Disable or hide a material in Unity. 0 Answers
Material darker in inspector then scene? 0 Answers