Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to check if the material is an Instance or an Asset?

Discussion in 'Scripting' started by Leonid, Apr 14, 2022.

  1. Leonid

    Leonid

    Joined:
    Aug 20, 2013
    Posts:
    90
    Hello!
    I'm changing materials of mesh renderers at a runtime when the character is frozen and when it gets back to normal.
    Frozen material is an asset, it is always the same, and the main material is an instance (created in a runtime).
    So I apply the material like this:
    Code (CSharp):
    1.        
    2. protected void SetMaterial(Material _material)
    3. {
    4.     if (!_material) { return; }
    5.     for (int i = 0; i < renderers.Length; i++)
    6.     {
    7.         renderers[i].material = _material;
    8.     }
    9. }
    10.  
    However, when I apply the frozen material, it would be better to use "renderers.sharedMaterial = _material;" to prevent memory leaks.
    Is there a way to check if the material is an Instance or an Asset?
    Thanks!
     
  2. Leonid

    Leonid

    Joined:
    Aug 20, 2013
    Posts:
    90
    There is a way to do this before the material is assigned, but there must be a more elegant solution.