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.
asked Nov 08 '10 at 06:43 PM
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 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 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
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:
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)
Do you really mean to swap materials or are you simply changing the material values which is a lot simpler?
answered Nov 08 '10 at 08:45 PM