Best practice to query a material/shader for a main color?

I have a script that modifies the “main color” of a GameObject. I do not have control over which materials and shaders have been assigned to the GameObject.

If I assign to material.color, it is not guaranteed to work since not all shaders support a “main color”:

  myGO.GetComponent<Renderer>().material.color = someColor;   // works in many cases

The problem is that I don’t know if the shader supports “_COLOR” (e.g. Unlit/Transparent and Unlit/Texture shaders do not publish a color). And, I want to prevent or warn the user that it will have no effect if used with this specific material/shader.

  1. What is the best practice for checking if the material/shader has a “main color”?
    (I assume I use “if (material.color == null)”, and if null, assume no main color. Or do I need to query the shader for its properties?).

  2. Also, I assume that the correct solution is to create a custom Inspector that checks the material/shader of the game object that my script is assigned to and display an error/warning if it has no main color.

Sounds like you’re looking for HasProperty.

if(material.HasProperty("_COLOR")) ...