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

Change color of material for a 3D augmented object.

Discussion in 'AR' started by zezozm2014, Aug 3, 2020.

  1. zezozm2014

    zezozm2014

    Joined:
    Aug 3, 2020
    Posts:
    1
    So i'm creating an augmented reality app that let's me place augmented furniture in a scene and im trying to create buttons that let me change the color of the furniture at runtime.

    Furniture model has a meshrenderer component which has only 1 material (colored red)attached to it.
    i'm trying to change it to a specific color by clicking a button which calls a method that changes the original red color to that specific color (let's say blue).

    I tried changing the color of the material of the models using this code:


    Code (CSharp):
    1. public void ChangeColorToBlue()
    2.         {
    3.             Renderer rend = SelectedObject.GetComponent<Renderer>();      
    4.             rend.material.shader = Shader.Find("Standard");
    5.             Color blue = new Color(0, 0, 255);
    6.             rend.material.SetColor("_Color",blue);
    7.  
    8.         }
    i also tried something like this.

    Code (CSharp):
    1. public void ChangeColorToBlue()
    2.         {
    3.          
    4.             MeshRenderer rends = SelectedObject.GetComponent<MeshRenderer>();
    5.             Material[] mats = rends.materials;
    6.             mats[0].color = Color.blue;
    7.             rends.materials = mats;
    8.  
    9.         }
    both of them did not work. i don't know where the problem is.
    i'm hoping someone could help me.
    thank you in advance.
     
  2. AjBaruah

    AjBaruah

    Joined:
    Mar 14, 2019
    Posts:
    8
    From the docs: "Note that a shader might be not included into the player build if nothing references it! In that case, Shader.Find will work only in the editor, and will result in pink "missing shader" materials in the player build."

    For more information: https://docs.unity3d.com/ScriptReference/Shader.Find.html
    Hope this helps!