I found already some things - but those seem not quite what I need. (perhaps outdated functions?)


here, it seems you can add a shader to a renderer:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Shader shader1;
    public Shader shader2;
    public Renderer rend;
    void Start() {
        rend = GetComponent<Renderer>();
        shader1 = Shader.Find("Diffuse");
        shader2 = Shader.Find("Transparent/Diffuse");
    void Update() {
        if (Input.GetButtonDown("Jump"))
            if (rend.material.shader == shader1)
                rend.material.shader = shader2;
                rend.material.shader = shader1;


well - i dont have a renderer. i only have a material.

and there is no "material.setShader" , only a material.setTexture"

sooo can anyone solve this mystery of how to add a shader to a material via script?

  • 1
    \$\begingroup\$ The example code you posted is quite obvious. It just accesses the material through the renderer. When you already have a reference to a material, you should be able to access its .shader property. Or is your actual question how to get a reference to a specific material in your project assets? \$\endgroup\$
    – Philipp
    Jul 8, 2016 at 13:13
  • \$\begingroup\$ ...and that cube has no renderer? When it is a default cube you created from the Unity context menu then it should have one. \$\endgroup\$
    – Philipp
    Jul 8, 2016 at 13:33
  • \$\begingroup\$ okay - so I have a material (in my Assets) I have a shader (in my Assets) If I drag and drop the Shader in the Material - it works. I want to do the same thing in the c# skript. Now I have another material (created in code) and a Shader (created in code). The big question: how to add that shader inside that material? Its gotta be 1 line of code..... \$\endgroup\$
    – OC_RaizW
    Jul 8, 2016 at 13:38
  • \$\begingroup\$ When you have a variable with your material called theMaterial and a shader with the variable theShader, it should just be theMaterial.shader = theShader. When that does not work, you should finally post your own code so we can see where your mistake is. \$\endgroup\$
    – Philipp
    Jul 8, 2016 at 13:51

1 Answer 1


If you already have a reference to the material and the shader then you just need to do this: myMaterial.shader = myShader; There's nothing more to it than that.

The Unity Material API says that the shader property is public so you can just use = instead of finding some "set" method. There is an explicit "set" and "get" function for textures on materials because they can have more than one so they're identified with a name or id.

In your example with the renderer, it's just getting the material from the renderer and doing the same exact thing I mentioned above. This code...

rend.material.shader = myShader;

has the same results as this code...

Material myMaterial = rend.material;
myMaterial.shader = myShader;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .