When you need to change a shader property but you don't know what to use for the proper name to use, select the material, click on its settings icon then click on "Select Shader". From there, you will see all the shader property names and their types and you will know which function and name to use to change the shader's properties.
With the default standard shader it looks something like this:
You need to know this otherwise you would need to ask new question for each property you want to change.
Your Renderer reference:
public Renderer rend;
To set it, the SetXXX
function is used:
rend.material.SetFloat("_Metallic", 1); //Metallic is a float
To get it the GetXXX
function is used:
float metallic = rend.material.GetFloat("_Metallic"); //Metallic is a float
To change or get the The albedo color to 255,0,0,255.
rend.material.SetColor("_Color", new Color32(255, 0, 0, 255));
Color color = rend.material.GetColor("_Color");
Notice that I used Color32
instead of Color
because Color32
takes values between 0
and 255
while Color
expects values between 0
and 1
.
To change the material's shader to "Unlit/Color", just find it then assign it to the material:
Shader shader = Shader.Find("Unlit/Color");
rend.material.shader = shader;
Note that the "Unlit/Color" shader doesn't have the _Metallic
property. It has one one property and that is "_Color". You can use the first method I described to determine which properties it has before attempting to change them.
What if the shader is used on many different objects. I want all the
objects to change when the property is changed on one of them.
To change all the objects using the-same material, use Renderer.sharedMaterial
instead of Renderer.material
. The shared material changes the original material and every other objects should pick that new material up as long as you have not called Renderer.material
on the material which actually makes a new copy for the said material and disconnects the renderer material from the original material.