How to switch texture of several 3D objects on GUI buttons click?

Hi @all,

The scene has some 3d objects with same texture. On mouse click on GUIbuttons, the 3dobjects will change to another texture. I made the materials and are already in the "materials list" of those objects. So, with the activation of one button, the objects should switch the "material 0" (current) for "material 1", clicking another button should change current for material 2 and so on. Will it help if all those 3dobjects are in the same gameobject or not?

As a fresh padawan in scripting, dont have code to post, even dont know if the script to achive that behaviour must be attached to objects or to the buttons.

Thanx in advance.

Your question fails to describe some key details. Also, I'm not sure if you are confused about the terminology and actually want to switch materials or if you simply wanted to swap a texture on some materials.

3dobjects in the same GameObject doesn't really make sense. Do you mean as children of the same GameObject or as part of the same mesh or something else entirely?

Textures:

Textures would be simply put as 2D images that are mapped to some coordinate space. The texture of a material can be treated like any other material property such as color and swapped the same.

Materials:

materials and sharedMaterial are the same, except changes to material will create a new material for just this object, whereas changes to sharedMaterial will change the material for all objects using the material. Anywhere in the following where it is written `.sharedMaterial`, you can substitute `.material` if you need. If you want to change the material for all objects using it, then you don't even need to know what the object is, but only need change the material that they share like so:

var target : Material;
var swapValue : Color = Color.white;
function OnGUI() {
    if(GUI.Button(...) && target) {
        var temp : Color = target.color;
        target.color = swapValue;
        swapValue = temp;
    }
}

If you wanted to change the materials for all instances of a prefab, you don't need to change the instances, but can make the changes to the prefab in much the same way as shared materials above.

A single object can have multiple materials if this is specified, but in general most objects will only use the first material, except in certain specific cases. Adding more materials to the materials arrays will not really serve much purpose if you aren't using them and swapping them like this isn't really the intention of the arrays, but to swap the values you could do something like:

//swapping index 0 and 1 in the materials or sharedMaterials array
var objects : GameObject[];
function OnGUI() {
    if(GUI.Button(...)) {
        for(go : GameObject in objects) {
            var current : Renderer = go.renderer;
            if(!current || current.sharedMaterials.Length < 2)
                continue;
            var temp : Material = current.sharedMaterials[0];
            current.sharedMaterials[0] = current.sharedMaterials[1];
            current.sharedMaterials[1] = temp;
        }
    }
}

In a more complete approach, you would do something like adding something to indicate the material to swap to in a script attached to the object or prefab and then call the function or apply the changes from the script.

swapMat.js (attached to the thing with a mat to swap)

var otherMaterial : Material;

function SwapMaterial() {
    if(otherMaterial && renderer && renderer.sharedMaterial) {
        var temp : Material = renderer.sharedMaterial;
        renderer.sharedMaterial = otherMaterial;
        otherMaterial = temp;
    }
}

swapButton.js

var objects : GameObject[];
function OnGUI() {
    if(GUI.Button(...)) {
        for(go : GameObject in objects) {
            var script : swapMat = go.GetComponent(SwapMat);
            if(!script) continue;
            script.SwapMaterial();
        }
    }
}

Do you really mean to swap materials or are you simply changing the material values which is a lot simpler?