Why can't you change a material's color during runtime after build?

I am simply trying to change the sprite renderer’s color to a random color when I click the button. This works 100% fine in editor play mode but only does not work when I build it. I’ve tried Color, Color32…I’ve tried everything and have found dozens of people with the same problem…never finding a solution.

 public Color32[] colors;
    int index;
    Color32 randomColor;
    
    void Start()
    {
       
        torso  = GameObject.Find("Torso").GetComponent<SpriteRenderer>();
       head = GameObject.Find("Head").GetComponent<SpriteRenderer>();
       armL = GameObject.Find("ArmL").GetComponent<SpriteRenderer>();
       armR = GameObject.Find("ArmR").GetComponent<SpriteRenderer>();
       
    }


public void RandomColor()
    {

        Color32[] colors = { new Color32(29, 204, 61, 255), new Color32(236, 159, 33, 255), new Color32(156, 190, 15, 255), new Color32(31, 134, 157, 255) };
        int index;
        index = Random.Range(0, colors.Length);
        Color32 randomColor = colors[index];

        torso.material.SetColor("_Color", randomColor);
        head.material.SetColor("_Color", randomColor);
        armL.material.SetColor("_Color", randomColor);
        armR.material.SetColor("_Color", randomColor);
}

Ultimately this is going to depend on the shader your material is using. If it’s built in render pipeline and the standard shader, then material.color alone should suffice. If it’s something else, then it will depend on the shader. Every shader uses a different name for its various parameters, and not all of them support a multiplicative blend color.

I’ve also tried it with every shader…on URP, URP off, etc. I’ve tried everything I could think of.

If you’re changing the shader, you will need to change the code to change the color every time you do that.

Pick one shader that you want to use, and if you need help with changing the color for that shader come back here. Most materials in URP use “_BaseColor” for this property: https://gamedev.stackexchange.com/questions/172151/how-to-change-material-color-lwrp

Yeah, I tried BaseColor with URP too. Ideally, I’d use the Default Sprite shader…That’s the only shader where i can change the sprite renderer’s color and have it accurately reproduced and bright. I just want it to change like this during build run:
https://www.youtube.com/watch?v=pFo9EFVo0u8

Try using “transform.GetComponent().color” instead of “material.setcolor”, maybe unity is using the spriterenderer color in the inspector instead of the material’s.

1 Like

oh gosh yeah - if this is a SpriteRenderer just use the color on the SpriteRenderer component!