Switching shaders without Shader.Find?

In my script I am switching between shaders like it shows how to in the scripting reference, with Shader.Find

This is pretty much how I use it my code, except I'm using a switch. Its for a ball select screen, many of the balls are the same sphere that just changes textures, and if I can get it to work, shaders.

This is basically how I have it work in my code

var shader1 : Shader = Shader.Find("Diffuse");
var shader2 : Shader = Shader.Find("Specular");

function Update()
{
    //ballChoose is just an int that increases / decreases when next or previous buttons are pressed

    switch(ballChoose)
    {
        case 1:
            standardBall.renderer.material.shader = shader1;
        break;

        case 2:
            standardBall.renderer.material.shader = shader2;
        break;
    }
}

This works fine in the editor, even though I get this error,

ArgumentException: Find can only be called from the main thread.

When I make a build, nothing happens, I just get a black screen, I think this error must be why because its the only error in my code.

After looking around I found out that you're only supposed to use Find inside of Awake or Start, but if I declare the variables in there then I cant use them in Update. So I was wondering if anyone knew of a way to change shaders without Find, or of any other way to make this work.

Thanks!

You declare the variables outside like you're doing now, then assign them in start

var shader1 : Shader;
var shader2 : Shader;

function Start() {
    shader1 = Shader.Find("Diffuse");
    shader2 = Shader.Find("Specular");
}

JavaScript hides what is going on. Any variables that you assign outside of a function are assigned using the parameterless constructor. As you saw, you cannot use Find in the parameterless constructor.

Do what Mike said, or something similar that will execute in the Editor. You can also download the shaders, put them in your project folder, and assign them with drag-and-drop, but although that's a fine method when you use custom shaders, using Shader.Find is less clutter, when the shaders are not in the Assets folder.