- Home /
Assign different materials to a range of same prefab?
this is surely basic, but the help files don't make sense to me, and can't find a matching answer in here.
I have a cube prefab. I create 5 of them at launch, each named uniquely, using an array that iterates through a for loop up to the 5. and, while placing them, I want to assign a different material to each one. The materials are named "instrument1" thru "instrument5" and currently in the Project.
Here's what I thought would be the manual/reference page to show how to do this, but it makes no sense to me.
http://unity3d.com/support/documentation/ScriptReference/Renderer-material.html
var materials is an array of Material[]???? which materials? What are their names? How are they being referenced/arrayed?
and why is their a "sharedMaterial", surely this is not being shared.
Answer by Eric5h5 · Dec 18, 2010 at 04:13 PM
Just assign a material to each prefab. It would be easier if you used an array of materials instead of separate variables. As for "which materials", they are whatever materials you assign in the inspector. They're referenced like any other array (by number).
var prefab : GameObject; var materials : Material[];
function Start () { for (i = 0; i < materials.Length; i++) { var clone = Instantiate(prefab); clone.renderer.material = materials[i]; } }
sharedMaterial is for assigning a single material to multiple objects, so it's shared. Changing the sharedMaterial of one object would cause the material on all of them to be changed.
Ok, I'm still stuck with the same problem. I want to assign a different material to each of the Instantiations of the one prefab. Otherwise, yes, I would apply the materials in the editor/inspector. So my question is still sort of the same. WHERE and WHAT are the materials that your array materials[] points to in the editor? Are they called material1, material2, material3? And where are they that the script can find them?
actually, one more thing/question. It appears "$$anonymous$$aterial" with a capital letter is a reserved word, from the color in your code. A class. I'm sorry, but the scripting components of materials and Unity have me completely baffled. Is beyond simplistic.
Please try to explain in non shorthand, where I'm wrong. You've created a variable called materials, and it contains all the information of all materials in the editor in an array the length of all materials in the editor??? Then you create enough clones of the object to useup all the materials in the editor?
Is this right?
If I make a range of materials so they're in a folder called "clone_materials" which sits nested inside a parent folder called "materials" in my project, and the materials are named clone_material_1, clone_material_2 ... thru 8. How do I assign each one of these materials each one of 8 clones of the same prefab?
The code I wrote already assigns a different material to each instantiation of the prefab. The materials are in the array, when you add them. If you don't add anything, the array is empty. Look at the script in the inspector, open the materials variable if necessary, and add items to it.
One more thing, Eric, THAN$$anonymous$$YOU for explaining shared$$anonymous$$aterial. That ter$$anonymous$$ology was doing my head in. Is there somewhere online the developers of Unity set out their methodology, rationality and reasoning behind their decisions about design of Unity?