Build problem, missing variables

(Sorry for my english)
I have a scene with 3 mesches. Each mesh has this script:

    using UnityEngine;  
    using System.Collections;
    
    public class Button : MonoBehaviour {
    
        public Shader shader1;
        public Shader shader2;
        public Object levelToLoad;
    
        void Start()
        {
            shader1 = Shader.Find("Diffuse");
            shader2 = Shader.Find("Self-Illumin/Diffuse");
            renderer.material.shader = shader1; //to include the shader Self-Illumin/Diffuse in the build
        }
    
        void Update()
        {
    
        }
    
        void OnMouseOver()
        {
            renderer.material.shader = shader2;
        }
    
        void OnMouseExit()
        {
            renderer.material.shader = shader1;
        }
    
        void OnMouseDown()
        {
            Application.LoadLevel(levelToLoad.name);
        }
}

I assign the script to each mesh and the scene to load to each script, but when i build and run the progect and i click on a mesh instead of load the assigned scene appear an error on the development console.

This is the error: NullReferenceException: Object reference not set to an istance of an object

How can i fix this error?

In a few words the problem is that the shaders dont get compiled in the build.

When Unity creates your build it compiles everything in the Resources folder but from the Assets folder only those objects are compiled that you have a reference in the compiled scenes or in the Resources folder.

For example if you have a material in the Resources folder that uses the “Self-Illumin/Diffuse” shader, this shader also compiles even if it is not in the Resources folder. Also if you put a scene in the build that has a gameobject that uses a material from the Assets folder and that material uses this shader the shader will be in your build.

But i guess you dont have a reference to the shader and it is not in the Resources folder. It is normal that it is working in the editor because all the resources are available in the editor.

The solution might be that you create materials in the Resources folder and use the needed shaders on them. Or just put the shaders in the folder but honestly I dont know where to find them. Also if you created the materials you dont need to use Shader.Find() then you can just use (Resources.Load(“materialName”) as Material) and you can assign them to the meshes.

One other note: If your meshes dont use the same material dont use renderer.material because it makes a copy of it. Use renderer.sharedMaterial instead.