Changing Texture in a GUI Box?

I currently have a variable set up like this:

var boxContents : Texture2D;

That works fine. What if I want to change the texture at runtime? I tried this:

var type1 : Texture2D;
var type2 : Texture2D;
var boxContents : Texture2d = type1;

if(whatever){
boxContents = type2;
}

Nothing shows up in the boxes. Any ideas?

Essentially your gui gets redrawn every frame in the OnGUI(){} function. So In order to swap out the textures, you essentially want to draw a different button.

Note: I am assuming you are handling all gui using unity calls in the OnGUI function, if your implementation is different, let me know.

var type1 : Texture2D;

var type2 : Texture2D;

var boxContents : Texture2d = type1;

 

// I am assuming this is in an update function or something
if(whatever){

boxContents = type2;

}

// Your box will be updated each frame with whatever boxContents is assigned to
function OnGUI () {	  
   GUI.Box(Rect(100,100,150,75),boxContents);
}

Again, I am working of several assumptions of your implementation… let me know if this helps, if not it would really help to see the contents of your OnGUI function.