Changing color on a material through script
Newish to unity here.
I need to change the color of a gameobject designed with a raycasthit.
Roughly:
RaycastHit hit;
hit.collider.GetComponent().GetComponent().SetColor("_white", Color.white);
Upon testing that code, I keep getting :
'' there's is no material attached to the gameobject, but a script is trying to access it''.
I drag-and-drop'd my material onto my gameobject, I tried creating public variable in the script ( of the game object, material, renderer... everything) and using the inspector to associate them... nothing.
I must be missing something obvious, yet can't see it.
Thanks for your time.
Answer by Positive7 · Sep 10, 2015 at 05:22 PM
Roughly :
void Update ()
{
RaycastHit hit;
Ray ray= Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit)) {
hit.collider.GetComponent<Renderer> ().material.SetColor ("_Color", Color.white);
}
}
So if mouse enters above object it's material color will change to white.
Thanks, the big problem : i used GetComponent().SetColor... ins$$anonymous$$d of .material.SetColor...
As expected, a simple detail that troubled me for waaay too long.