How to change the texture of an object in script.

It’s like Mincecraft. I’ve made a prefab and I want to make it so that the selected texture from the menu (in the game) is used on the cube that is created. So example:
[YOUR PLAYING THE GAME]
You press “M” and a menu pops up with different textures. You choose the the texture you want and then your press “SPACE” and a cube is created with that texture.

Thats the best I can explain it…

Don’t know if it’s kind of same like I was asking these days… Check this out

var btnTexture : Texture;
var bumpMap : Texture;
function OnGUI() {
    if (!btnTexture) {
        Debug.LogError("Please assign a texture on the inspector");
        return;
    }
    if (GUI.Button(Rect(10,10,50,50),btnTexture))
        Debug.Log("Clicked the button with an image");
    if (GUI.Button(Rect(10,70,50,30),"Click")){
        Debug.Log("Clicked the button with text");
        renderer.material.mainTexture=bumpMap;
    }
}

Put this script on a cube for example and for the textures apply the texture you want to be displayed on the cube. Note that there are two textures that need to be applied, one will be applied to the cube and the other is for the GUI button.

Hope it helps.
Cheers.

A GameObject can have a Renderer which in turn have one or more Materials which in turn have Textures.

To change the main texture of the material attatched to a game object’s renderer (JavaScript):

var texture : Texture;
gameObject.renderer.material.mainTexture = texture;

This is all explained thorougly in the unity script reference.