adding a shader to a game object

How can i add a shader to a game object? a have a shader defined here:

(i am trying to do the same thing and am having some problems): getPixel/setPixels or stencil eraser brush - Questions & Answers - Unity Discussions

and 2 texture2D defined in code and loaded through Resource.Load, assigned to a game object. How can i apply the shader to my game object and communicate with the two textures?

As you can see, i have this code in my shader

 _MainTex("Main Texture (rgb)", 2D) = "white" {}
        _Color ("Main Color", Color) = (1,1,1,1)
       _Stencil("Stencil Texture (a)", 2D) = "white" {}

how can i tell the shader who is Main Texture and Stencil Texture?

I hope I’m not misunderstanding what you’re asking, but as far as I understand what you’re trying to do it is possible to set the texture of your material with:

        renderer.material.SetTexture("_MainTex", referenceToYourTexture);

setting the “_Stencil” texture will work exactly the same way.

You can apply the shader in the Unity editor to the material you will be using by changing the .shader value of the material:

        renderer.material.shader = shader2;