Search Unity

When setting an array of materials, only the first on is an instance?

Discussion in 'General Graphics' started by Comrad_Squirrel, Aug 8, 2020.

  1. Comrad_Squirrel

    Comrad_Squirrel

    Joined:
    May 28, 2016
    Posts:
    20
    Hi all,

    I am using the URP and want to set a material that uses a Shader Graph dissolve shader to a renderer with multiple materials. This is my code:

    Code (CSharp):
    1.             var dissolveMaterials = new Material[originalMaterials[i].Length];
    2.             //Create a new array of materials of the length of the original materials, set all these to dissolve and give them the colour of the original material
    3.             for(int j = 0; j < dissolveMaterials.Length; j++)
    4.             {
    5.                 dissolveMaterials[j] = dissolveMaterial;
    6.                 dissolveMaterials[j].SetColor("Albedo", originalMaterials[i][j].color);
    7.                 activeMaterials.Add(dissolveMaterials[j]);
    8.             }
    9.             //Set this new array
    10.             rendererList[i].materials = dissolveMaterials;
    Where dissolveMaterials is the array with the dissolve shader material I want to set (with different colours).

    My strange problem is, that while the materials and their colours are set, only the first material is (Instance), the other is shared... This is a problem, as this means I can't set individual colours or update the shader clip threshold of the not-Instance materials.

    I have tried setting the dissolveMaterial with new or Instantiating it, but this doesn't help.

    Does anyone know why only the first material is an Instance, or how I can get the others to become an Instance?

    Thanks in advance!
     
  2. Comrad_Squirrel

    Comrad_Squirrel

    Joined:
    May 28, 2016
    Posts:
    20
    Shameless bump.