Log in or register to post comments

Script MaterialSwitch.cs

December 26, 2019 - 12:09pm #1

Hello guys,

I found the script MaterialSwitch.cs in the Vuforia Unity Samples at advanced model targets application.

That script is attached to the KTM bike model in Unity. I don't know what this script does and when it is called cuz

it has no start / update function.

Can some1 help me understand what this script does and when it is called.




Copyright (c) 2018 PTC Inc. All Rights Reserved.

Vuforia is a trademark of PTC Inc., registered in the United States and other



using System.Collections;

using UnityEngine;

public class MaterialSwitch : MonoBehaviour


    #region PUBLIC_MEMBERS

    public Material[] materials;

    #endregion // PUBLIC_MEMBERS


    private int materialIndex = -1;

    #endregion // PRIVATE_MEMBERS

    #region PUBLIC_METHODS

    public void SwitchMaterial()


        if (this.materials == null || this.materials.Length == 0)




        // Increase the material index


        if (this.materialIndex >= this.materials.Length)


            this.materialIndex = 0;


        // Set the material

        var renderers = this.GetComponentsInChildren<MeshRenderer>();

        foreach (var renderer in renderers)


            renderer.material = this.materials[materialIndex];

            if (renderer.material.HasProperty("_ScanTime"))


                renderer.material.SetFloat("_ScanTime", 0.0f);

                StartCoroutine(UpdateScanTime(renderer.material, 1.0f));




    #endregion // PUBLIC_METHODS


    IEnumerator UpdateScanTime(Material mat, float duration)


        float time = 0;

        float deltaTime = 0.05f;

        var wait = new WaitForSeconds(deltaTime);

        while (time < duration)


            if (mat.HasProperty("_ScanTime"))


                mat.SetFloat("_ScanTime", time);


            time += deltaTime;

            yield return wait;



    #endregion // PRIVATE_METHODS


Script MaterialSwitch.cs

January 8, 2020 - 2:19am #2


In order to determine where a specific asset is being used, you can right-click on it and Select Find References in Scene.

MaterialSwitch.cs is attached to the KTM prefab and is being used as an effect to "scan" the model.

Thank you.

Vuforia Engine Support

Log in or register to post comments