CodeBlanks Logo Icon CodeBlanks

Assigning a material

Learn to assign a material to a Mesh and about sub-meshes.

Assigning Materials to MeshRenderer

A Material determines how the surface of a 3D model appears (color, texture, glossiness). Through the material or materials property on the MeshRenderer component, materials can be accessed and modified dynamically in code.

MeshRenderer renderer = GetComponent<MeshRenderer>();
renderer.material.color = Color.red;

Submeshes and Multiple Materials

Although complex 3D models appear as a single geometry, they are split into Submeshes so that different parts can use different materials. For example, a car's body uses metallic material while its windows use transparent material. In this case, the renderer.materials array holds a material for each submesh.

Material[] allMaterials = renderer.materials;
Debug.Log("Material count: " + allMaterials.Length);

Practice Read Material Count

Assign renderer.materials.Length to the sayi variable. Then print it with Debug.Log("Malzeme: " + sayi);.

Starter code

Player.cs
using System;

public class Material
{
    public string name;
    public Material(string name) { this.name = name; }
}

public class MeshRenderer
{
    public Material[] materials = new Material[] { new Material("Gövde"), new Material("Cam") };
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        MeshRenderer renderer = new MeshRenderer();
        // Atanmis malzeme sayisini alip konsola yazdir
    }
}

Sample solution

Player.cs
using System;

public class Material
{
    public string name;
    public Material(string name) { this.name = name; }
}

public class MeshRenderer
{
    public Material[] materials = new Material[] { new Material("Gövde"), new Material("Cam") };
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        MeshRenderer renderer = new MeshRenderer();
        int sayi = renderer.materials.Length;
        Debug.Log("Malzeme: " + sayi);
    }
}

Expected output

Malzeme: 2

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which property path is used to change the color of an object's current material in code?

  • renderer.material.color Correct answer
  • meshFilter.color
  • transform.material.color
  • renderer.mesh.color

Explanation: To change material color, the color variable of the material property on MeshRenderer is accessed.

2 Multiple Materials Array

Question
Material[] liste = renderer.____;
Answer
Material[] liste = renderer.materials;

Explanation: The array of all materials assigned to an object is accessed via the materials property.

3 Material Terminology

Term Meaning
renderer.material The object's first or single material
renderer.materials Array of all materials on the object
Submesh Sub-geometry taking different materials
material.color Base surface color of the material

All lessons in Intermediate