Why can't I set my object's color?

Why won't my color change with this line of code?

transform.renderer.material.color = Color.black;

It simply refuses to change... why?

The only reason it shouldn't work is that your shader doesn't have a property called "_Color". Open up the shader and make sure. Here's what you'll use if the color property has a different name, and you don't want to edit the shader:


Also, you don't need to call two components like that.

renderer.material.color = Color.black;  // transform. is not necessary.

This code is giving me error “object reference not set to an instance of an object”

//This is working code for change color I did it

using UnityEngine;

using System.Collections;

public class Tile : MonoBehaviour {

public Vector2 gridPos = Vector2.zero;
Renderer r;

// Use this for initialization

void Start () {
     r = GetComponent<Renderer>();

// Update is called once per frame
void Update () {


void OnMouseEnter()
    r.material.color = Color.black;
    Debug.Log("X pos = "+ gridPos.x + "Y pos = "+ gridPos.y);