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

Changing material colors was working, now it is not?

Discussion in 'Scripting' started by Pinkuboxu, May 5, 2020.

  1. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    Ok, it's done this to me at least twice now. I rewrote it and it worked perfectly for a while but it stopped working. I'm getting a log of the renderer.materials[] and it is changing the colors, but for whatever reason the renderer isn't displaying the changes. I swear this worked twice before but for whatever reason it stops working. I'm going to rewrite the script again and see if it does this again. Meanwhile maybe there is something wrong that someone else can find? I'm just using the standard pipeline with the standard shader. renderer.sharedMaterial.SetColor (or .color = ) gives the effect however since there are multiple instances not sharing the Materials it needs to work how it was. I must swear that I didn't change anything.

    I've tried using the <Renderer> type as well with no results.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterAppearance : MonoBehaviour
    6. {
    7.     // appearance values based on gradients
    8.     //[Header("Appearance Values")]
    9.     public Gradient skinGrad;
    10.     public Gradient eyesGrad;
    11.     public Gradient hairGrad;
    12.  
    13.     //[Header("Gender / Body Types")]
    14.     //[Space(10)]
    15.     public Mesh[] gender;
    16.     //Material[] materials;
    17.  
    18.     SkinnedMeshRenderer rend; // <This was just Working...
    19.  
    20.     void Start()
    21.     {
    22.         rend = GetComponent<SkinnedMeshRenderer>();
    23.  
    24.         RollAppearance();
    25.     }
    26.  
    27.     public void RollAppearance()
    28.     {
    29.         rend.material.color = Color.red; // nerp...
    30.         RollGender();
    31.         RollColors();
    32.     }
    33.  
    34.     void RollGender()
    35.     {
    36.         rend.sharedMesh = gender[Random.Range(0,2)]; // this works...
    37.  
    38.     }
    39.  
    40.     void RollColors()
    41.     {
    42.         // materials = rend.materials; // save old
    43.         // ... whatever, just incase I need this?
    44.         // rend.materials = materials; // restore old
    45.  
    46.         rend.materials[0].SetColor("_Color", skinGrad.Evaluate(Random.Range(0f,1f)));
    47.  
    48.         rend.materials[1].SetColor("_Color", hairGrad.Evaluate(Random.Range(0f,1f)));
    49.  
    50.         rend.materials[3].SetColor("_Color", eyesGrad.Evaluate(Random.Range(0f,1f)));
    51.  
    52.         // DEBUG: the rend.materials[].color are changing...
    53.  
    54.         print("StartColors");
    55.         foreach (var mat in rend.materials)
    56.         {
    57.             print("*** Material: " +mat.name+ " is of color: " +mat.color+ " ***");
    58.         }
    59.         print("EndColors");
    60.         print("");
    61.     }
    62. }
    63.  
     
  2. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    Just like that It was working after I moved the line that I commented // nerp... into start... but then it stopped working again with on the second run, didn't change a thing! Do I need to refresh something or clear a cache or something stupid? This is ridiculous.
     
  3. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    Kind of bumping but also just confirming that it sometimes works and mostly doesn't.

    I've got a button that calls the RollAppearance(), but it also should work when it runs. If it does change color when the game starts then the button works. I've even sat there running and stopping the game several times but that's not it.. I rewrote this every possible way I could think and it wasn't working. But I loaded Unity back up again today and it was working at first. Then it stopped working again. The material color data is getting set (material.color for index 0 as well as 1 and 3 are returning new values each time, it's changing the gender mesh, but the renderer isn't reflecting it the color change. I'm not sure what to make of it as it seems like a bug with the renderer and not something I'm doing. Like an update needs to happen with the renderer that isn't happening. The following is the code as it was when it started working earlier and when it stopped working. I'm not touching it again as I'm fairly certain this should work... and I mean it has... but it isn't now... or maybe it is but it won't again!

    I doubt this will help but maybe:
    upload_2020-5-6_0-55-45.png upload_2020-5-6_0-57-20.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterAppearance : MonoBehaviour
    6. {
    7.     // appearance values based on gradients
    8.     [Header("Appearance Values")]
    9.     public Gradient skinGrad;
    10.     public Gradient eyesGrad;
    11.     public Gradient hairGrad;
    12.  
    13.     [Header("Gender / Body Types")]
    14.     [Space(10)]
    15.     public Mesh[] gender;
    16.  
    17.     SkinnedMeshRenderer rend;
    18.  
    19.     void Start()
    20.     {
    21.         rend = GetComponent<SkinnedMeshRenderer>(); // Needed because <Renderer> doesn't access the Mesh
    22.         RollAppearance();
    23.     }
    24.  
    25.     public void RollAppearance()
    26.     {
    27.         RollGender();
    28.         RollColors();
    29.     }
    30.  
    31.     void RollGender()
    32.     {
    33.         rend.sharedMesh = gender[Random.Range(0,2)];
    34.     }
    35.  
    36.     void RollColors()
    37.     {
    38.         // materials = rend.materials; // save old
    39.         // ...
    40.         // rend.materials = materials; // restore old
    41.  
    42.         rend.materials[0].color = skinGrad.Evaluate(Random.Range(0f,1f));
    43.  
    44.         rend.materials[1].color = hairGrad.Evaluate(Random.Range(0f,1f));
    45.  
    46.         rend.materials[3].color = eyesGrad.Evaluate(Random.Range(0f,1f));
    47.  
    48.         // DEBUG:
    49.  
    50.         /*print("StartColors");
    51.         foreach (var mat in rend.materials)
    52.         {
    53.             print("*** Material: " +mat.name+ " is of color: " +mat.color+ " ***");
    54.         }
    55.         print("EndColors");
    56.         print("");
    57.         */
    58.  
    59.     }
    60. }
    61.  
     
  4. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    Heh, I still have this problem....
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    This is the problem. You want to either:

    a) create a fresh array of material references, fill them out with Materials, then assign it back to the renderer

    OR

    b) copy out the array, make the changes you want, and put it back.

    I prefer option b generally, but you may have a reason for the other.

    It can look something like this:

    Code (csharp):
    1. var tmp = new List<Material>( rend.materials);
    2.  
    3. tmp[0].color = ...
    4. tmp[1].color = ...
    5. tmp[2].color = ...
    6.  
    7. rend.materials = tmp.ToArray();
    Or you can use good old System.Array.Copy, which is probably a little bit "lighter," not sure, I'm just lazily typing in this website editor window.
     
    kdgalla and Vileogames like this.
  6. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    Thank you for the response. I'll hopefully try it soon and respond, and not just give up on things for a long time... it's a hard time right now.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    I'm sorry to hear that, but... it's a great time to expand your brain! I hope you have success changing your multi-material renderers.