tadeseus
tadeseus

Reputation: 75

MaterialPropertyBlock doesn't change colors at runtime?

I'm drawing a lot of boxes with different colors. For that I'm using MaterialPropertyBlocks

public override void DrawPlot()
{
    for (int posIdx = 0; posIdx < data.Length; posIdx++)
    {
        plotModelInstances[posIdx] = GameObject.Instantiate(plotModel, ...);

        _propBlock = new MaterialPropertyBlock();
        _renderer = plotModelInstances[posIdx].GetComponent<Renderer>();
        _renderer.GetPropertyBlock(_propBlock);
        _propBlock.SetColor("Color", MiscUtils.GetColor(data[posIdx].Value / maxValue, StaticValues.jet));
        _renderer.SetPropertyBlock(_propBlock);
    }
}

This function is part of a (non-Monobehaviour) class. The problem is, that if I run the programm, all blocks are white. Is that because I use MaterialPropertyBlocks outside of a MonoBehaviour script?

Upvotes: 1

Views: 1865

Answers (1)

tadeseus
tadeseus

Reputation: 75

Found the problem: The property is called "_Color" and NOT "Color" This works

_propBlock.SetColor("_Color", MiscUtils.GetColor(data[posIdx].Value / maxValue, StaticValues.jet));
currentBar.GetComponent<MeshRenderer>().SetPropertyBlock(_propBlock);

Upvotes: 3

Related Questions