- Home /
Assign material to multiple objects
Is there a way to assign a material to multiple objects at once?
Q
Answer by Marnix · Apr 06, 2011 at 05:06 PM
Write a foreach
loop and assign the material to all your gameObjects
. You have to specify something to create your list. Something like FindChildren
or maybe something with tags. That all depends on what you want to find of course.
So i am getting the feeling that a lot of the things i want to do like basic things that i personally think should be in the editor need to be done via scripts.... Strange but i guess i had better just get over it...
Thanks!!
@quentin: If you want this to happen in the editor, you will see that a lot of problems arise. What do the selected items do with the new material? Replace the old? Or add an extra material? It's too specific to just say: assign.
Well if i drop a material on a single object it replaces the existing one.... just like i wanted it to. But if i have 20 of them ins$$anonymous$$d of being able to just select the 20 which is no problem if i drop a material on that group it just does the one where it is dropped... not rocket science i would not think... but it just seems that all the little tweaking things that i want to do like rotate the uvw etc all has to be done with a script. I guess i would say that a basic set of controls is missing from the editor..
But who wants to write a script for something like the ability to put a material on multiple objects even though it would save you 20 $$anonymous$$ I am sure once i get into the habit of writing little scripts for everything maybe i wont think twice..
@quentin: You could always ask Unity for a support on these little features. It seems indeed that something simple like this would be appreciated. For most of it, I don't like it to be in editor, I prefer the scripts. This will keep an overview of what is actually there and what is done. If everything is already set inside the scene, you lose grip of your scene. Someone can't read what you did, because it's all binary. There is no way to find back what is in your scene-files. Creating worlds in a procedural way keeps your project clean.
Answer by o_oll · Nov 10, 2012 at 08:20 PM
In the Editor it's as simple as adding a component to multiple objects:
Select all the objects you want to modify in the Hierarchy.
Drag the Material you wish to assign from the Project View to the Inspector(Beneath any existing components).
For me this applies the Material to all selected objects.
Note: It does not work that way if you drag the material to the Scene View, in that case it only gets applied to the one your mouse is over, it needs to be the inspector.
I am not sure if this was true with older Unity versions (like the one when the question was asked), but I totally agree that currently this is the way to do it.
I should only notice that this procedure (multi-editing) works, when ALL the selected objects share the same to be edited components. In this case, it's the mesh renderer component, which needs to be shared by ALL the selected objects. There's even a warning appearing on the Editor, when objects without common components are selected.
In addition, the editing doesn't apply, if the material is assigned to the parent objects directly. If, for instance, an object (or multiple of them) with a mesh component is parented to an empty object, then the object with the mesh itself should be selected, in order its material to be edited and not the parent.
This said, I assume that the difficulty, anymore, is to select all these objects you want to edit. Though I guess (unless there's no worry to be done by script) some proper hierarchy structure could solve this issue.
Please correct me if I'm wrong.
Is there a way to mesh multiple objects into one object so when I add a material it is consistent throughout all the objects? I have multiple gameobject pieces that make up the "carpet" but the multiple carpet materials next to each other doesn't look right. I don't think it's a tiling issue. Sometimes when the camera hits the right spot it blinks in and looks normal. ![alt text][1] [1]: /storage/temp/103266-carpetissue.jpg
Answer by Saraf · Jun 06, 2014 at 08:25 AM
Select all the Objects (In the Scene/Hierarchy), then drag and drop the wished material in the Inspector. I don't know it that matters, but I did this just under the "Add Component" button.
Hope this will help :)
Thanks Saraf!
The trick is to drag the material beneath the "Add Components" button, as you suggested. If you don't, it won't work!
Here I was thinking I had already tried all possible permutations to get this to work :\
Answer by shaan24 · May 23, 2016 at 02:05 PM
I wrote the following code that helped me. Just save the following code in a C# file named "AssignMaterial". This adds a Assign Material option in the GameObject menu.
using UnityEditor;
using UnityEngine;
using System.Collections;
public class AssignMaterial : ScriptableWizard {
public Material material_to_apply;
void OnWizardUpdate ()
{
helpString = "Select Game Objects";
isValid = ( material_to_apply != null );
}
void OnWizardCreate ()
{
GameObject [] gos = Selection.gameObjects;
foreach( GameObject go in gos )
{
Material[] materials = go.GetComponent<Renderer> ().sharedMaterials;
for ( int i = 0 ; i < materials.Length ; i++ )
materials [ i ] = material_to_apply;
go.GetComponent<Renderer> ().sharedMaterials = materials;
materials = go.GetComponent<Renderer> ().materials;
for ( int i = 0 ; i < materials.Length ; i++ )
materials [ i ] = material_to_apply;
go.GetComponent<Renderer> ().materials = materials;
}
}
[MenuItem("GameObject/Assign Material", false, 4)]
static void CreateWindow ()
{
ScriptableWizard.DisplayWizard ("Assign Material", typeof(AssignMaterial), "Assign");
}
}
I hope this helps!
Answer by MohdGasim · Apr 10, 2011 at 09:24 PM
HI...my quick fix was to assign a material to one of the objects. Then modify it as needed. Then drag it onto each object. This way you wont need to re-adjust the UV etc...hope this helps
Well that is kind of how i do it now actually... But my current model is a mechanical assembly and there are tons of small objects all hidden here and there. I think normally i would not have a problem with it but i did not export the model myself or i would have set up the materials better in $$anonymous$$ax... i will do it next time. And i did notice in the Unity Store that there are a few scripts for doing exactly what i was asking to do so there you go, just buy an editor script or write one i think is my answer
well...it might be still easier to just re-import the file in max..fix it up..and then export to FBX including the textures (embed media)
I have the same problem. But $$anonymous$$e is a city to I have hundreds of buildings. It is a point dragging the material to every object.