Unity Change Color of Material

This week I have a super quick tip for you. If you need to change the color of an object in Unity, it is pretty straight forward.

First you need to get a reference to the object’s Renderer component. A good place to do this may be in the Start method:

Renderer rend;

// Use this for initialization
void Start () {
  rend = GetComponent<Renderer>();
}

Then inside of the Update method, you could change the the color (like when the Mouse button was just pressed):

void Update () {

  if (Input.GetButtonUp("Fire1"))
  {
    rend.material.SetColor("_Color", Random.ColorHSV());
  }

}

That really is all there is to it. If you want a little more information on why the magic string of _Color is used, make sure to check out the video this week:

Tell me what you think!