How to create a GUI button to change a character's texture in real-time?

Hi, I’m trying to have a button on my screen that will change the texture of my character if the user presses that button. the material with the texture i want to swap is on my gameObject called “joint1”.

I don’t really know what I’m doing… artist trying to do whatever sounds basic in a game-engine…

This is my code at the moment:

var material1 : Material;
 

function Update () {

    for (GUI.Button(Rect(Screen.width - 74,148,64,64)))
	{
        GameObject.Find("joint1").renderer.sharedMaterial = material1;
    }
}

Try this …

#pragma strict

var myTexture : Texture2D;
private var go : GameObject;

function Start(){

	go = GameObject.Find("joint1");
}

function Update () {

	if (GUI.Button (Rect (20,40,80,20), "Texture")) {
		go.renderer.material.mainTexture = myTexture;
	}
}

Here’s the script I came up with eventually. I didn’t know how to swap the textures so… MAJOR THANK YOU!!! I will get to doing it that way tomorrow to save on materials.

I decided to go with a toggle instead of a button.

=============================================================

// Make the script also execute in edit mode
@script ExecuteInEditMode()

var matIcon : Texture2D;
var material1 : Material;
var material2 : Material;
var recPosX = 1;
var recPosY = 1;

var toggleBool = true;

function OnGUI () {
    var toggleBoolNew = GUI.Toggle (Rect ( recPosX , recPosY , 100, 64), toggleBool, GUIContent (matIcon, "Toggle"));

    // Check if the toggle was toggled
    if (toggleBoolNew != toggleBool) {
        if (toggleBoolNew == true)
            GameObject.Find("joint1").renderer.sharedMaterial = material1;
        else
            GameObject.Find("joint1").renderer.sharedMaterial = material2;

        toggleBool = toggleBoolNew;
    }
}