How can I assign materials using C# code?

I'm trying to assign a material in the Project to a gameobject I create by code. My question is how can I do that directly instead of creating a new Material, asign a shader, textures, etc.

1 Like

Using a material that has already been creaded and using it at runtime without dragging it in the inspector can be done with Resources.Load. Within your Asstest folder you will have to create a Resources folder and place your material there.

// Assigns a material named "Assets/Resources/DEV_Orange" to the object.
Material newMat = Resources.Load("DEV_Orange", typeof(Material)) as Material;
gameObject.renderer.material = newMat;

You can set or change the material of an existing GameObject (if the GameObject has a renderer), using the Renderer.material property, like this:

GetComponent<Renderer>().material = newMaterialRef;

Of course using this method means that you need a reference to an existing material, so you need to create one in your Assets (Library) first, and then reference it in your script. You can do this by creating a public variable of type 'Material' like this:

public Material newMaterialRef;

And then dragging a reference of the new material into that variable slot in the inspector.

If you want to create a new material from scratch in code, you can do that too. To do this, you can just create a new Material object like this:

Material myNewMaterial = new Material( shader );

Where 'shader' is a reference to an existing shader to use. The example given in the manual here also has an example where a new shader is created at the same time, from source code included as text.

Hope this is enough to get you started. If you have more specific questions, feel free to ask as a comment to this answer!

If somebody is searching for this answer recently. This might be useful

    public GameObject box;
    public Material red;
    private void OnTriggerEnter(Collider other)
    {
         if (other.gameObject.tag == "Pickupable")
         {
              box.GetComponent<Renderer>().material = red;
         }
    }

Maybe someone want to explain what is up with this $$anonymous$$ stuff. I see this type of thing all the time. It appears to me, that the developer of this website, needs to allow for this, because this $$anonymous$$ stuff makes the comment almost totally unreadable, or perhaps the person making the comment DOES NOT KNOW how to post the comment (really??? How about that).
Seems to me this comment says:
MeshRenderer renderer = gameObject.GetComponent();
Material materials = new Material[renderer.sharedMaterials.Length;
etc, etc, etc
One can determine that $$anonymous$$ is replacing the M, but why does anyone want to spend time thinking this way? I thought the point was to find answers to questions, and not go thru this SPELLING CRAPOLA

hieshRenderer renderer = gameObject.GetComponent(); $$anonymous$$aterial[] materials = new $$anonymous$$aterial[renderer.shared$$anonymous$$aterials.Length]; for (int i = 0; i < renderer.shared$$anonymous$$aterials.Length; i++) { materials *= my$$anonymous$$aterial; } renderer.shared$$anonymous$$aterials = materials;*

What if your model has more than one material slot? I have a robot hand with two materials that I need to swap via code. (I appreciate any answers ahead of time, ya’ll rock)