Search Unity

I need to change the color of a child and not the games material color

Discussion in 'Prefabs' started by giantkilleroverunity3d, Feb 12, 2019.

  1. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    This line of code changes the game's color so that any child that uses it all change to the same new color:
    Code (CSharp):
    1. tsChild.GetChild(0).GetChild(0).GetChild(0).GetComponent<Renderer>().material.color = new Color(64, 0, 0);
    I am trying to get to the instance of a single child's color and cannot seem to find a solution. Most answers I have seen only deal with one child so the mass change is never seen.
    Thank you.
     
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    Hi.

    This line must change color for only one renderer.
    So if it changes all renderers then you have set somehow the same material object into material property for many renderers, and then accessing this material object changes object and all renderers that use it have chaged.

    Simple solution will be:

    Code (CSharp):
    1. var renderer = tsChild.GetChild(0).GetChild(0).GetChild(0).GetComponent<Renderer>();
    2.  
    3. var newMaterial = new Material( renderer.material );
    4. newMaterial.color = new Color(64, 0, 0);
    5.  
    6. renderer.material = newMaterial;
    7.  
    or:


    Code (CSharp):
    1. var renderer = tsChild.GetChild(0).GetChild(0).GetChild(0).GetComponent<Renderer>();
    2.  
    3. var mpb = new MaterialPropertyBlock(); //cache it
    4. mpb.SetColor("Color", new Color(64, 0, 0));
    5.  
    6. renderer..SetPropertBlock( mpb );
    7.  
    or better find a reason of same material on many renderers in their material property and use MaterialPropertyBlock :)
     
  3. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    I found something interesting in the color assignment:
    When the:
    Code (CSharp):
    1. tsChild.GetChild(0).GetChild(0).GetChild(0).GetComponentInChildren<Renderer>().material.color = new Color(128, 0, 0);
    2.            
    See attached upload below.

    The two dark bars are because the color(red,0,0) is 0 else all the other colors are the highest of bright as in 255 but I never set that. My data is always 1 through 255.
    The green is the backgound. My screen shots will look better when I get the colors set correctly.

    If each instantiated prefab has its own shader but that color is added from the project tab, how can I tell if there are separate colors? I am thinking each GO has its own entirely.
     
    Last edited: Feb 14, 2019
  4. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    2019-02-13 13_10_00-Unity 2018.2.2f1 Personal (64bit) - cuboid.png

    And this is with my line of code or your suggestion.
     
  5. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    Color is property of material, shader is property of material too.
    Material is property of Renderer.

    So changing some thing inside material will change it for each renderer that use that material.

    When you first access Renderer.material property you create copy of material stored in Renderer.sharedMaterial property, this is not performant, but must work.

    You can loop against all your renderers and compare it material with default one.
    And additionally you ca change it in inspector to understand how many object change their color when you change color of only one of it.
     
  6. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    I believe the color is changing for each child. The problem I found is the color(r,g,b) is not functioning correctly. I will post a new thread asking about this.
    As you can see from the screen shot the bars are changing. It is the color() statement that is acting weird.
    Thanks.
     
  7. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    Used the color() which the x,y,z are .01 to 1.0. Switched to Color32(). I typed the incoming int to (byte)industryColor and it worked.