assign a material to multiple object?

Hi,
i try to select multiple object and try to assign a material to that but the last object get that material.
so how can i assign a material to multiple object?

What do you mean by selecting multiple obect?
Maybe you should make an array of GameObjects, and assign material to elements of that array!

You can’t apply a material to several selected objects at once, they have to be set individually.

It’s possible with an Editor Script. Save this as AssignMaterial.js and put in a folder named Editor.

class AssignMaterial extends ScriptableWizard
{
    var theMaterial : Material;
    
    function OnWizardUpdate()
    {
        helpString = "Select Game Obects";
        isValid = (theMaterial != null);
    }
    
    function OnWizardCreate()
    {
        
	    var gos = Selection.gameObjects;
	
	    for (var go in gos) 
	    {
	        go.renderer.material = theMaterial;
	    }
    }
    
    @MenuItem("Custom/Assign Material", false, 4)
    static function assignMaterial()
    {
        ScriptableWizard.DisplayWizard(
            "Assign Material", AssignMaterial, "Assign");
    }
}

thanx ! sluurp ! :smile:

This is an old post, but I wanted to thank you for the script. Seriously cut down on material assignment.

This is handy. To be honest I wish unity did more things to multiple selections much like 3dsmax does.

In 3ds max if you select multiple objects and go to the equivalent of the inspector, you can change settings for all selected objects globally.

If you have a selection where a checkbox or whatever is not the same on all objects then the current setting is greyed out but you can overwrite these settings globally by changing it and it will no longer be greyed out.

Perhaps it’s worth suggesting this behaviour as a wish list. It really speeds things up in the editor if you can do global settings on a multi selection of entities. Having to do the same task multiple times on seperate individual items is slow and cumbersome and takes an awful long time.

Umh,

Sorry to bring back this old thread, but is there a way to get this to work with 3.0? It won’t compile…

Works fine in 3.0 here – make sure that you specify it’s a javascript, though.

thanks for this, it is very useful

thanks for the script !! based on your idea I made to others … :stuck_out_tongue:
one to change a prefab from the selected objects, and other to rename with sequential numbers

class AssignPreFab extends ScriptableWizard
{
    var thePrefab : GameObject;
    
    function OnWizardUpdate()
    {
        helpString = "Select Game Obects";
        isValid = (thePrefab != null);
    }
    
    function OnWizardCreate()
    {
        
	    var gos = Selection.gameObjects;
	
	    for (var go in gos) 
	    {
	        go.GetComponent(zombiegen).otherUserType = thePrefab;
	    }
    }
    
    @MenuItem("Custom/Assign Prefab", false, 4)
    static function assignMaterial()
    {
        ScriptableWizard.DisplayWizard("Assign Material", AssignPreFab, "Assign");
    }
}

the other that renames

class RenameWithNumber extends ScriptableWizard
{
    var namePreffix: String;
        
    function OnWizardUpdate()
    {
        helpString = "Rename with numbers";
        isValid = (namePreffix != null);
    }
    
    function OnWizardCreate()
    {
        
	    var gos = Selection.gameObjects;
		var counter :int = 1;
		
	    for (var go in gos) 
	    {
	        go.name = namePreffix + counter++;
	    }
    }
    
    @MenuItem("Custom/Rename with numbers", false, 5)
    static function rename()
    {
        ScriptableWizard.DisplayWizard("Rename with numbers", RenameWithNumber, "rename");
    }
}

How do I use this script?

Some help on how to run this would be awesome!

For those of you who can’t make this work:

-Create a javascript script
-Call it “AssignMaterial”
-Create an “Editor” folder in the Assets folder if it isn’t there already
-Place script in that folder
-This will create a new menu along the top “Custom”
-Click Custom/Assign Material
-A pop up box will come up with a place to drop the material
-Select the objects
-Click Assign

Hope this helps out

Here is the c sharp version:

using UnityEngine;
using System.Collections;
using System;
using UnityEditor;

public class AssignMaterial : ScriptableWizard 
{

	public Material theMaterial;
    String strHelp = "Select Game Objects";
    GameObject[] gos;
    
    void OnWizardUpdate ()
    {
        helpString = strHelp;
        isValid = (theMaterial != null);
    }
    
    void OnWizardCreate ()
    {
        gos = Selection.gameObjects;
        foreach (GameObject go in gos)
        {
            go.renderer.material = theMaterial;
        }
    }
    
    [MenuItem ("Custom/Assign Material", false, 4)]
    static void assignMaterial()
    {
        ScriptableWizard.DisplayWizard ("Assign Material", typeof(AssignMaterial), "Assign");
    }
}

If I don’t get the question wrong, I think what you are looking for is simply by selecting all the objects from the Hierarchy window, and drag the material that you want to apply to the Inspector instead, NOT the Hierarchy.

Also take note that you can’t apply directly to parented objects.

Hope this helps.

3 Likes

the assign material script just crashes unity 4.0
did somoen manage to makeit work with 4 ?

This script is obsolete as you can now edit multiple selections at the same time.

Yes you can select multiple game objects and change values in the Inspector, you still can’t drag and drop a material on multiple selected objects in the inspector.

and I just used this script in 4.3.1 with no issue what so ever.

Let me re-iterate the steps listed above that I JUST used successfully.

For those of you who can’t make this work:

-Create a javascript script
-Call it “AssignMaterial”
-Create an “Editor” folder in the Assets folder if it isn’t there already
-Place script in that folder
-This will create a new menu along the top “Custom”
-Click Custom/Assign Material
-A pop up box will come up with a place to drop the material
-Select the objects
-Click Assign

Biggest pitfall I think people are having is they are making a C# script.

Thanks for the great scripts! I wondered if there is an easy way to select all objects within a prefab, in order to run the AssignMaterial.js script in one go. A character hierarchy can be pretty complex with lots of nodes on a character that is a jointed model.

Is there a method or script by which I can select the top node of a prefab, click a button or run a script and then have all geometry objects below it selected so that I can run AssignMaterial.js on them?

So far, the best way I have found is to filter in the hierarchy using t:objects. This at least narrows down the number of objects to sift through.