Get Texture

I have a script attached to an object. Before this last modify script work correctly, but I did manually assign the texture. The last added is the part of code that can read texture from object material. (This is only a part of script, with last change):

var btnTexture : Texture;
    
function Start () {
btnTexture = renderer.material.GetTexture ("_MainTex");
}


function OnGUI () {
windowRect = GUI.Window (0, windowRect, DoMyWindowT, "");
}

function DoMyWindowT (windowID : int){
    GUI.DrawTexture(Rect(0,0,windowRect.width,windowRect.height), btnTexture, ScaleMode.ScaleToFit, true, 0f);
}

The script can read main texture object and use in GUI.DrawTexture, but when I start the scene unity say me that texture is not assigned and crash.

UnassignedReferenceException: The variable btnTexture of ‘GUI Object’ has not been assigned.
You probably need to assign the btnTexture variable of the GUI Object script in the inspector.

Somebody help me?

Are you using a custom shader that actually has a property called “_MainText”? Because otherwise the built-in shaders all use “_MainTexture”, though you might as well just use renderer.material.mainTexture. I’m not sure why you’re assigning it as a public variable if you’re just going to re-assign it in Start though.

More on this: http://forum.unity3d.com/threads/40389-Load-image-problem

Don’t try to set your texture pointer directly. Use SetTexture.