- Home /
Accessing multiple texture materials on objects
How to get to and modify the separate parts of a multi textured object ? For example, I have a tapered cube created in my 3D modeling app. The 6 sides have all been separately assigned textures, named Topside, Bottomside, Left, Right, Front and Back. All of these are re-created as Materials when exported to Unity.
In Unity, how can I access e.g the Front Material and change it's Color property from within a script ?
Answer by hijinxbassist · Mar 08, 2012 at 11:29 PM
If you want to access each material on your object, you can use something like this..
function Awake()
{
var materials:Array=renderer.materials;
if(tag=="Enemy")
{
materials[0].color=Color.black;
materials[1].color=Color.red;
}
}
I stumbled on this post and figured i add my 2 cents(or materials in this case)
I did a test on this(only thing in the script)using #pragma strict. Seems it wont work the way i laid it out. By removing the var materials and replacing the 'materials' vars with the actual line renderer.materials fixes it.
#pragma strict function Awake() { if(tag=="Enemy") { renderer.materials[0].color=Color.black; renderer.materials[1].color=Color.red; } }
Futher update to my answer since Unity has added #pragma strict in the beggining of ALL new scripts(not a bad idea really!). Here is how my first example would be written in strict typing for all you out there in cyberland!
#pragma strict
var mat:$$anonymous$$aterial[];
function Awake()
{
mat=renderer.materials;
}
Then you can just refer to them as
mat[0].mainTexture=myTexture;
mat[1].color=myColor;
Hope this helps some of you out. Feel free to +1 if i saved your day and prevented further hair-pulling! LOL
Answer by DaveA · Nov 23, 2011 at 05:14 PM
The import process should have created 6 different Materials right? So you just change them like this http://unity3d.com/support/documentation/ScriptReference/Material-color.html
Front.color = new Color(.2,.5,.1);
How do I access the material labelled as 'Front' ? Is it assigned as a variable first ? The imported object has assigned all the 6 materials, but I can't get to their names without an error being thrown.
Answer by Vienna · Nov 24, 2011 at 09:38 PM
You can access the materials like this http://unity3d.com/support/documentation/ScriptReference/Renderer-materials.html
So for example:
var mats = go.renderer.materials;
for (mat in mats) {
mat.color = Color.red;
}
Your answer
Follow this Question
Related Questions
Material with two textures 0 Answers
Set Material Texture Border 1 Answer
Cannot implicitly convert type `UnityEngine.Texture2D' to `UnityEngine.Material' 2 Answers
Random Tiling Textures 0 Answers
Unity Materials and UV coordinate help 3 Answers