- Home /
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.
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?).
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.
Answer by tanoshimi · Sep 06, 2016 at 06:19 AM
Sounds like you're looking for HasProperty.
if(material.HasProperty("_COLOR")) ...
Your answer

Follow this Question
Related Questions
How should I code for multiple types of enemy with specific action set? 1 Answer
Custom map editor running slow due to too many draws. 0 Answers
If-else statement executing incorrectly in foreach loop,why? 0 Answers
TouchControls/MouseInput HELP PLEASE 0 Answers
Performance: Multiple asset references vs. One public asset reference 2 Answers