GUIButton

Hi Guys,

Is there any easy way to change the texture on a GUIButton after a press on the Button.

I have to randomly chose the textures from a list

Since GUI is immediate mode, there is really nothing to “change”. Each OnGUI, you draw the button from scratch – you would just need generate a new random value for to select from an array of images when the button returns true, and use that image, for example.

I guess I tried something similar. But the texture doesnt seem to change at all.

This is

if(GUI.Button (Rect (0,Screen.height - 75, 50, 60), controlTexture1)){

		GUI.Button (Rect (-50,Screen.height - 75, 50, 60), controlTexture6);
	}

Do help me out if am doing something stupid. I am very new to Unity3D as well as prgramming.

This is what I was thinking – I didn’t test it. When GUI.Button returns true, you change the reference to the image. Next time OnGUI is called, it is referencing a new image. In this, I’m selecting randomly from a list of textures.

// a list of textures
var controlTextures : Texture[];
// an integer
var button : int = 0;
OnGUI(){
   // pulled the Rect out to make it fit better on the forum
   var r : Rect = new Rect(0, Screen.height - 75, 50, 60);
   // our button was pressed
   if (GUI.Button (r, controlTextures[button]){
      // get a new random value for the texture
      button = Random.Range(0, controlTextures.Length);
   }
}

Thanks a lot !!!