How to change a gameObject's material via scripting?

How would I change a gameObject's material via scripting?

First of all, a GameObject can't have a material. It can, however, have a Renderer, which has a Material: http://unity3d.com/support/documentation/ScriptReference/Renderer-material.html

So basically:

renderer.material = myOtherMaterial;

Use:

var texture : Texture;

function Start () {
    renderer.material.mainTexture = texture;
}

To change the texture to the material exposed in the inspector. For further material and texture documentation follow this link.

OK, let’s say we want to change the color of the material of current game object which this script attached to :slight_smile:
all you need is this line :slight_smile:

GetComponent<Renderer>().material.color = Color.cyan;