Change color of a GameObject when it collisions c#

Hello, I have a problem, I want a red cube to change color to yellow when it collisions with a capsule. So i did this:

using UnityEngine;
using System.Collections;

public class collisionpill : MonoBehaviour {

	public Color mycolor;

	void OnCollisionEnter(Collision other) {
		if (other.transform.tag == "Pill") {
			transform.GetComponent<Renderer> ().GetComponent<Material> ().color = mycolor;


So I assigned the “public color mycolor” to yellow. It doesn’t give me any errors, but when my cube collisions with the pill it says: "There is no material attached to the cube, but a script is trying to access it. You probably need to add a Material to the GameObject “cube”.

Material is a property of Renderer and not a component so your statement to assigning color should be:

 gameObject.GetComponent<Renderer> ().material.color = mycolor;