- Home /
Access properties of Material through script.
Hello, I would like to know how could I change properties of material (as on the image below) through script.
This script does not work:
gameObject.GetComponent<MeshRenderer>().material.SetColor("First Color", new Color32(0, 0, 0, 255));
Thank you very much.
Answer by Dragate · Oct 27, 2017 at 05:39 PM
Are you sure the color parameter is called "First Color"? Idk much about shaders, but many times they are actually called with an underscore, like "_First Color". Press the small cog icon (on the right side of your material name) , choose the option "select shader", you should now see the variables of the shader. On the left side are the names you should reference to.
Answer by tomix1024 · Oct 27, 2017 at 05:59 PM
Have a look at the Properties section of the shaders source code. The text displayed in the Inspector does usually not coincide with the actual name of the properties. They are usually declared like this:
_SpecColor("Specular", Color) = (0.2,0.2,0.2)
Where _SpecColor
is the name you want to use in your script and "Specular"
is the string displayed in the Inspector.
Answer by WilderMedeiros · Nov 25, 2021 at 05:22 PM
You can also verify if that variable exists, before trying to access it, like the example below:
if (RenderSettings.skybox.HasProperty("_Tint"))
RenderSettings.skybox.SetColor("_Tint", Color.red);
else if (RenderSettings.skybox.HasProperty("_SkyTint"))
RenderSettings.skybox.SetColor("_SkyTint", Color.red);
Source: https://stackoverflow.com/questions/12551768/unity3d-change-skybox-color-via-script from Codemaker
More info: https://docs.unity3d.com/ScriptReference/Material.SetInt.html
Your answer
Follow this Question
Related Questions
UI Triangle Color picker 0 Answers
SpriteRender.color not changing the color 1 Answer
Create Staining effect 0 Answers