- Home /
Is this an efficient way of changing a material by script?
I am trying to figure out if there's a more efficient way of changing an object's material by script, in this case when the camera detects if there's an object between the camera and another object. The script I've done so far is:
if(Physics.Raycast(transform.position, -(transform.position-target.transform.position), hit)){
if(hit.collider.gameObject.name != target.name){
//hit.collider.gameObject.renderer.enabled =false;
cont = true;
gravName = hit.collider.gameObject.name;
hit.collider.gameObject.Find(hit.collider.gameObject.name).renderer.material = mat2;
//Debug.Log("Hit");
}
if(hit.collider.gameObject.name == target.name && cont==true ){
gameObject.Find(gravName).renderer.material = mat1;
}
Some pictures:
The script works, but I noticed some lag, so if anyone could point me into the right direction, I would really appreciate it. Thank you.
Your answer
Follow this Question
Related Questions
Best way to tell which object(s) are being looked at? 3 Answers
Is there a shader in Unity that prevents transparent parts from overlapping? 1 Answer
Camera rotate to look at GameObject from Raycast 3 Answers
How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers
Make character look at mouse position, not world position 3 Answers