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

How TO Change the material of certain element in Mesh Renderer?

Discussion in 'Scripting' started by wonde, Jun 17, 2015.

  1. wonde

    wonde

    Joined:
    Jun 10, 2015
    Posts:
    5
    Hello, I though I would get answer/help here quicker than in Ask Question-section since I guess this is not really a question.
    So I need help with my script since I have many mesh renderers in my project with i.e. 7 elements and I need to change second and third elements to different material by simply pressing GUI-button and other materials needs to stay as such.



    Here is part of the script where you can see main structure of it:

    Code (JavaScript):
    1.  var Part : GameObject;
    2. var Part2 : GameObject;
    3. var Part3 : GameObject;
    4. var Part4 : GameObject;
    5. var mat2 : Material;
    6. var mat3 : Material;
    7. var mat4 : Material;
    8. var mat5 : Material;
    9. var matD : Material;
    10. function OnGUI()
    11. {
    12.      if(GUI.Button(Rect(120, 40, 80, 20),"Black"))
    13.      {
    14.          Part.GetComponent.<Renderer>().material = mat2;
    15.          Part2.GetComponent.<Renderer>().material = mat2;
    16.          Part3.GetComponent.<Renderer>().material = mat2;
    17.          Part4.GetComponent.<Renderer>().materials[3] = mat2;
    18.      }
    As you can see I've tried materials[3] as to choose that part third element, but it doesn't work. Script itself continues from that point with different materials for different GUI-button, so I guess those are not relevant to be here.
     
    AlterMannn and Devrokk like this.
  2. wonde

    wonde

    Joined:
    Jun 10, 2015
    Posts:
    5
    anyone?
     
  3. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    As the API says:http://docs.unity3d.com/ScriptReference/Renderer-materials.html
    You can't just change it like you tried up there. Maybe someone can take over or I will look into it later.
     
  4. wonde

    wonde

    Joined:
    Jun 10, 2015
    Posts:
    5
    Thanks for your reply.
    But if I can't change it in the way I've tried, I would appreciate if someone could help me to create working solution for it, because I really need to be able to change materials of those certain parts/elements by click of a gui-button.
     
  5. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,209
    if what asd234w4r5 is saying is the problem you need to

    Code (csharp):
    1.  
    2. Material[] mats = Part4.GetComponent.<Renderer>().materials;
    3. mats[3] = mat2;
    4. Part4.GetComponent.<Renderer>().materials = mats;
    5.  
    I don't think you need to work that hard just to change a material though... your code is just doing nothing? the way the API is worded you do need to do that I guess.... pull it out, change it, put it back
     
  6. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
  7. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
  8. wonde

    wonde

    Joined:
    Jun 10, 2015
    Posts:
    5
    @malkere and @asd234w4r5 thanks both of you, I will try these now and see if I get that working.
     
  9. wonde

    wonde

    Joined:
    Jun 10, 2015
    Posts:
    5
    Well I've tried to wrap my brain around this and I don't seem to understand how I am going to integrate that @malkere piece of code into my code. I've tried putting it here and there but it doesn't work and I'm pretty sure that's all because of my coding skills aren't really efficient. So if you could provide little more help I would appreciate that. And to mention I use javascript in this part and provided code is c#.
     
  10. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,209
    I'm sorry I'm not familiar with Javascript personally.

    What are you having trouble understanding? What the API is stating is that you cannot -access- the material array directly. If you call on it a copy will be created for you to look at. Thus, no matter what you do to it it will not change as you're only changing a copy.

    So, you have to swallow that and 1, take the copy -> 2, change the copy -> 3, then tell the original array to become the copy you've changed. That's what the code I wrote up is doing in lines 1, 2, and 3.

    I'm sure JS writes it the same order, just slightly different wording.
    Does that make sense?
     
  11. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    Hope this helps

    Code (csharp):
    1.  
    2. public GameObject humanRenderMesh;
    3. public Renderer ren;
    4. public Material[] mat;
    5.  
    6. void Start () {
    7.    ren = humanRenderMesh.GetComponent<Renderer>();
    8.    mat = ren.materials;
    9.    mat[0].color = Color.red;
    10.    mat[1].color = Color.blue;
    11.    mat[8].color = Color.black;
    12. }
    13.  
     
    Last edited: Feb 1, 2018
  12. darveshi

    darveshi

    Joined:
    May 19, 2017
    Posts:
    9
    Simply great answered.
     
    Anikita likes this.
  13. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    FINALLY SOME RECOGNITION!!!
    no problem.
     
  14. Arealight

    Arealight

    Joined:
    May 4, 2017
    Posts:
    29
    But with your script you only change the color, how do you change the materials? Materials Element [0], Element [1], Element [2], Element [3], Element [4], Element [5]
     
  15. sky_is_tumbling

    sky_is_tumbling

    Joined:
    Feb 26, 2018
    Posts:
    8
    Code (CSharp):
    1.  
    2. public GameObject humanRenderMesh;
    3. public Renderer ren;
    4. public Material[] mat;
    5.  
    6. public Material example;
    7.  
    8. void Start () {
    9.    ren = humanRenderMesh.GetComponent<Renderer>();
    10.    mat = ren.materials;
    11.    mat[0] = example;
    12.    mat[1] = example;
    13.    mat[8] = example;
    14.  
    15.    ren.materials = mat;
    16. }
    17.  
     
  16. andreiseniuc710

    andreiseniuc710

    Joined:
    Mar 1, 2020
    Posts:
    1
    This won't work btw
     
    rmele09 and RoterPokeball like this.
  17. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    697
    it worked for me