Search Unity

how to color a certain element .. [meshrenderer]

Discussion in 'Scripting' started by carnag45, Oct 22, 2019.

  1. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    Hello, I am trying to color a certain element of a meshrenderer, but with .color, I made several methods, nothing else works

    Code (CSharp):
    1.             method1
    2.             for (int x = 0; x < obj.Length; x++)
    3.             {
    4.                 Color color = obj[x].material.color;
    5.                 color[2] = redcolor;
    6.                 obj[x].material.color = color;
    7.             }
    8.  
    9.             method2
    10.             for (int x = 0; x < obj.Length; x++)
    11.             obj[x].material[2].color = redcolor;
    12.            
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,322
    The name of the color variable in the shader is probably not "_Color", which Material.color uses.

    You'll need to look at the material / shader you're using and figure out the actual name, then use Material.SetColor.
     
  3. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    It's not just about coloring, I'm trying to insert a color in the specific element, the setmaterial and material.color works, but the problem I'm having and inserting that color in a specific element
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,630
    What do you mean by "element"?
     
  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,322
    Perhaps you need to use Renderer.materials and target a material with a specific index on the MeshRenderer?
     
  6. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    nothing no, it's just a test I'm trying to do. the element is an int, so I'll set a value there in the inspector without having to change it every time in the script
     
  7. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    So I tried that too, but it works only with material and not with .color
    but I haven't found a solution yet

    Code (CSharp):
    1.     public MeshRenderer[] obj;
    2.     public Color redcolor;
    3.  
    4.     void Start()
    5.     {
    6.         for (int i = 0; i < obj.Length; i++)
    7.         {
    8.             Color colors = obj[i].material.color = redcolor;
    9.             colors[2] = redcolor;
    10.             obj[i].GetComponent<MeshRenderer>().sharedMaterial[2].color = colors;
    11.         }
     
    Last edited: Oct 22, 2019
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,630
    I dont know what you're talking about. Can you describe what exactly you are trying to modify?
     
  9. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    I was trying to change the color of a submesh, but I use getcomponent a lot, and for performance that's bad, do you have any tips to help?

    Code (CSharp):
    1.         if(index == 0)
    2.         {
    3.             for (int i = 0; i < obj.Length; i++)
    4.                 obj[i].GetComponent<MeshRenderer>().materials[2].color = redcolor;
    5.         }
    6.         else if(index == 1)
    7.         {
    8.             for (int i = 0; i < obj.Length; i++)
    9.                 obj[i].GetComponent<MeshRenderer>().materials[2].color = bluecolor;
    10.         }
    11.         else if(index == 2)
    12.         {
    13.             for (int i = 0; i < bloco.Length; i++)
    14.                 obj[i].GetComponent<MeshRenderer>().materials[2].color = greencolor;
    15.         }
    16.         //below has +30 indexes all with getcomponent '-'