---
title: "Swapping Materials at Runtime"
content_type: tutorial
url: "https://learn.unity.com/course/basic-reflect-customization/unit/adding-new-viewer-functinoality/tutorial/swapping-materials?language=en"
language: en
container_type: course
container_title: "Basic Reflect Customization "
container_url: "https://learn.unity.com/course/basic-reflect-customization.md?language=en&version=2019.3"
unit_title: "Adding New Viewer Functionality"
unit_url: "https://learn.unity.com/course/basic-reflect-customization/unit/adding-new-viewer-functinoality?language=en&version=2019.3"
standalone_markdown_url: "https://learn.unity.com/tutorial/swapping-materials.md?language=en&version=2019.3"
previous_title: "Creating a Construction Sequence Viewer"
previous_url: "https://learn.unity.com/course/basic-reflect-customization/unit/adding-new-viewer-functinoality/tutorial/creating-a-construction-sequence-viewer?language=en&version=2019.3"
previous_markdown_url: "https://learn.unity.com/course/basic-reflect-customization/unit/adding-new-viewer-functinoality/tutorial/creating-a-construction-sequence-viewer.md?language=en&version=2019.3"
next_title: "Creating Interaction in a Scene"
next_url: "https://learn.unity.com/course/basic-reflect-customization/unit/adding-new-viewer-functinoality/tutorial/creating-interaction-in-a-scene?language=en&version=2019.3"
next_markdown_url: "https://learn.unity.com/course/basic-reflect-customization/unit/adding-new-viewer-functinoality/tutorial/creating-interaction-in-a-scene.md?language=en&version=2019.3"
difficulty: intermediate
duration_minutes: 15
unity_version:
  - "2019.3"
supported_unity_versions:
  - name: "2019.4"
    url: "https://learn.unity.com/tutorial/swapping-materials.md?language=en&version=2019.4"
  - name: "2019.3"
    url: "https://learn.unity.com/course/basic-reflect-customization/unit/adding-new-viewer-functinoality/tutorial/swapping-materials.md?language=en&version=2019.3"
last_updated: "2026-08-04T14:24:51.000Z"
---

# Swapping Materials at Runtime

Write a C# script that swaps an object’s materials at runtime to change its appearance dynamically.

In this tutorial we’ll explore how to swap an object’s Materials at runtime via a C# script.

## Materials

- [SwappingMaterialsResources.zip](https://unity-connect-prd.storage.googleapis.com/20200521/6888b2f4-b00f-43c2-bb5f-98f8fef832ea/SwappingMaterialsResources.zip)
- [SwappingMaterialsComplete.zip](https://unity-connect-prd.storage.googleapis.com/20200521/e0f8cb2f-26a3-4733-b91f-27dd454402fa/SwappingMaterialsComplete.zip)

## 1. Introduction

In this tutorial we’ll explore how to swap an object’s Materials at runtime via a C# script.

Materials can be manually changed in a Scene just like any GameObject. (Note that Materials changed during runtime will revert back to the original after the project ends.) Materials manually changed in the Editor will remain unless the Reflect project is updated, in which case it will revert back to the Materials imported from Revit.

Changing Materials at runtime allows you to build functionality that can highlight certain design decisions or the current status of an object in the viewer and is a more scalable approach than manually changing objects.

For this tutorial, you will need:

- The Reflect package installed via the Package Manager
- A Pro license for the Unity Editor
- The Unity custom package SwappingMaterials.unitypackage that forms part of this tutorial.

## 2. Setting Up

**1.** Install the Unity Reflect package using the Package Manager.

**2.** Drag the Reflect Prefab from **Packages** > **Unity Reflec**t > **Runtime** (**Figure** **01**) into the Hierarchy (**Figure** **02**).

![Figure 01: The Reflect Prefab](https://connect-mediagw.unity.com/h1/20200521/learn/images/ab36fb06-18c6-4d12-8f90-f2def1b7e703_image2.png)

![Figure 02: The Prefab added to the Scene](https://connect-mediagw.unity.com/h1/20200521/learn/images/7f8d0d91-0725-47df-8aa6-b4ccca13e932_image8.png)

**3.** Download and import the custom package SwappingMaterials.unitypackage that’s provided as exercise files. This package contains a script for swapping the Materials as well as a test Material called _RedMaterial_ that you can use.

## 3. Adding the Script to the Scene

**1.** Create a new empty GameObject in the Scene Hierarchy by right-clicking and selecting **Create Empty** (**Figure** **03**).

![Figure 03: Creating a new empty GameObject](https://connect-mediagw.unity.com/h1/20200521/learn/images/4d70d50c-313c-4de0-ac2e-87282e334e03_image4.png)

**2.** Rename this new object to _ChangeMaterials_ (**Figure** **04**).

![Figure 04: Change the name of the empty GameObject](https://connect-mediagw.unity.com/h1/20200521/learn/images/4b3d15f1-b038-4791-b22d-a373649215e1_image1.png)

**3.** Drag the SwapMaterials.cs script from **Assets** > **Scripts** onto the new ChangeMaterials object (**Figure** **05**).

![Figure 05: Assigning the script to the new object](https://connect-mediagw.unity.com/h1/20200521/learn/images/fd24ed95-740d-49bf-8151-06e93af15168_image7.png)

**4.** With ChangeMaterials selected in the Hierarchy, go to the Inspector. You will see that there are two slots in the newly applied script, one for Category To Change and one for New Material (**Figure** **06**). The Category To Change slot is where you specify which category of object will be changed. This can be hardcoded into the script if you want, but this way you can experiment with it. The New Material slot is where you specify which Material will be applied as an override at runtime. You can create your own Material or use the RedMaterial provided in the Assets folder. To apply the Material, you drag into the **New Material** slot.

![Figure 06: The script’s user accessible slots in the Inspector](https://connect-mediagw.unity.com/h1/20200521/learn/images/d38bbf14-1af2-495f-80b0-326074e13036_image6.png)

**5.** Run the Reflect viewer and open any of your projects.

**6.** While the project is running, type in a category of object you would like to override. Note that the category needs to match the spelling and case exactly as it is in your Revit project.

**7.** Right-click on the script component gear icon and select **Change Materials** (**Figure** **07**).

![Figure 07: Running the script](https://connect-mediagw.unity.com/h1/20200521/learn/images/c86eff75-3a66-4492-b62f-75fcb30c7191_image3.png)

**8.** Your model will now update to reflect the new Material applied to all objects of your specified category. In the example below, the RedMaterial was applied to all objects of the category Floors (**Figure** **08**).

![Figure 08: The script in action](https://connect-mediagw.unity.com/h1/20200521/learn/images/32cf1fc3-e4f9-4f2a-8c99-319631cacfd9_image5.png)

## 4. Understanding the Script

The script used in this project is extensively commented to allow you to understand what each step does. Open it and read through the comments to gain a better understanding of how everything fits together.

As a general overview, the script takes the loaded model and iterates through everything to build a list of all the metadata. It then finds all the objects that have the specified metadata key — in this case it’s “Category.”

The script then loops over the list. When it finds an object with the category, it applies the new Material.

## 5. Next Steps

You should now have an understanding of how to access the metadata of a loaded object. The script could be linked to a button in the Scene by creating a new UI element, but it should be run only after the complete model is loaded. Running it early may result in some objects being missed.

Selecting objects by metadata is a powerful option that will allow you to customize your Reflect applications.
