Can't GET "_Color" through Skinned Mesh Renderer

My shader has “_Color” reference. I even tried the standard shader. I CAN SET the color, but CANNOT GET the color.

if (skinnedMeshRenderer != null){
            skinnedMeshRenderer.GetPropertyBlock(color);
            Color newCol = color.GetColor("_Color");
            newCol.a = alpha;
            color.SetColor("_Color", newCol);
            skinnedMeshRenderer.SetPropertyBlock(color);
}

The same code works for a regular Mesh Renderer, but not the skinned mesh renderer. I even used material.HasProperty and that returns true. GetColor can’t find “_Color” on skinned mesh renderers… or any color for that matter as I tried them all…

I’m using Unity 2021.3.16f1

Before

After

For now I’m using this work around

if (skinnedMeshRenderer != null){
            skinnedMeshRenderer.GetPropertyBlock(color);
            Color newCol = skinnedMeshRenderer.material.color;
            newCol.a = alpha;
            color.SetColor("_Color", newCol);
            skinnedMeshRenderer.SetPropertyBlock(color);
}

Well, the documentation says:

Since a PropertyBlock represents per-instance overrides, if there are no overrides, it does not contain any values at all. So if you currently have no per-instance overrides, your “workaround” seems to be the correct way of getting the color.

Note that a PropertyBlock is just a data collection, essentially some sort of dictionary that can hold any property overrides you want to use / apply for an instance. Getting the PropertyBlock only gives you the overrides, not every value in th shared material that the renderer uses. At least that’s what I get from the documentation.

2 Likes

Also note that calling .material will create a copy (new instance) of the material.
If you’re looking for a ‘default value’, you should be grabbing it from .sharedMaterial instead.

2 Likes

Ah, I see. That’s correct and makes sense. Thanks for clearing that up! :slight_smile:

I totally forgot about that. That’ll be much more optimal. Thanks! :slight_smile:

1 Like