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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Updating multiple materials on one mesh only works after clicking in Inspector?

Discussion in 'General Graphics' started by timothyallan, Jul 7, 2015.

  1. timothyallan

    timothyallan

    Joined:
    May 22, 2013
    Posts:
    72
    Sooo, I have an object with two materials on it. I create and store a reference to a new material, and can set the one color just fine by doing this pseudocode:

    Code (csharp):
    1.  
    2. material1Copy = new Material(GetComponent<Renderer>().sharedMaterial)
    3. material1Copy.color = whatever
    4. GetComponent<Renderer>().material = material1Copy;
    5.  
    However, when trying to change material 1 AND 2 like

    Code (csharp):
    1.  
    2. material1Copy = new Material(GetComponent<Renderer>().sharedMaterials[0]);
    3. material2Copy = new Material(GetComponent<Renderer>().sharedMaterials[1]);
    4.  
    5. material1Copy.color = whateverColor1
    6. material2Copy.color = whateverColor2
    7.  
    8. Material[] myMaterials = new Material[] { material1Copy, material2Copy };
    9.  
    10. GetComponent<Renderer>().sharedMaterials = myMaterials;
    11.  
    It doesn't appear to work in the game view... but if I click on the object in the inspector while the game is playing, and click on the circles next to the materials, they update and magically display the right colors.

    What am I missing?

    This is in 5.1.1 using the standard shader.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
  3. timothyallan

    timothyallan

    Joined:
    May 22, 2013
    Posts:
    72
    Enabling keywords was the ticket, I needed to .EnableKeyword("_EMISSION") and everything magically started working.

    Thanks Karl
     
    karl_jones likes this.
  4. inewland

    inewland

    Joined:
    Dec 6, 2012
    Posts:
    20
    Thanks this helped a lot!

    Note that if you are using the Standard Shader and trying to apply to '_BumpMap', you have to use '_NORMALMAP'.
     
  5. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    5
    i am facing the same issue in Unity 5.6.1 and .EnableKeyword() is not helping...
    I want to apply AOMap on the materials. AOMap and materials are in the asset bundle..
    Please check my code -:
    temp.EnableKeyword("_DETAIL_MULX2");
    temp.SetTexture("_DetailAlbedoMap", aoTexture);
    temp.SetFloat("_UVSec", 1.0f);