How to change texture of individual GUI.Button ?


I have a texture array and I iterate a for loop through set of elements to generate a button with the image from texture array. My goal is to change an individual button’s texture that was generated with the loop.

Following is my scenario-drawing. I only want the single Button1’s texture to be changed however I do something wrong…

Here is my code that generates Button1s and I call it at the OnGUI(), whenever I click on Button1 all Button1s change the texture :

public int k = 0; = new Vector2(scrWidth / 18f+i*scrWidth/3.5f, backButPos.y/ 1.5f);
for(int i = 0; i<size; i++)

	if(GUI.Button(rectBut, textures[k], guiSkin.customStyles[0]))
			if (k >= textures.Count ()-1)
				k = 0;
				k += 1;

It may be a longshot but I will try to explain what can be done. This functionality is useful if you use Unity UI components instead of Scripting GUI.

  1. Create a folder called ‘Resources’ in your Assets, then put all your textures in the Resources>(Create New)Textures folder. All those textures should be in the form of Sprites, as buttons need sprites, not raw texture files to work.
  2. As I work in C#, I know this functionality only but you can find on net how to port various function to UnityScript. Create a new script, then create an array of sprites named thumbnails, and then fill it up with those textures by using thumbnails = Resources.LoadAll<Sprites> ("Textures"). The “Textures” field is basically the path where you have stored the files inside the ‘Resources’ folder.
  3. Create a UI panel using Unity UI components (I have notices that you are using Scripting GUI, but trust me, this will sort many of your UI problems), attach a Layout Group Component to it (horizontal or vertical or both) and a Content Size Fitter component to it. Set it’s values as per your choice (it’s all iterative development).
  4. Create a Button prefab, or in your case, one of those drawings, with the big Rectangle and buttons below it. Set it’s properties as per your choice.
  5. In your script, get the button prefab in a public gameObject variable, get the UI panel as an another gameObject component, and inside a for loop, Instantiate the button prefab and set it as the child of the Unity UI panel. The UI panel acts a container for these buttons and with the Layout Group component and Content Size Fitter component, all these buttons are sorted and arranged in a geometrical way.
  6. In that same for loop, after a button in instantiated and set as child of the UI panel, access the Button1 of the button prefab, then access it’s Image component by (buttonPrefab).GetComponent.sprite and assign it the value of the thumbnails array. (pathToButton1OfButtonPrefab).GetComponent<Image>.sprite = thumbnails*, this will then assign the particular sprite at the array index of thumbnails to the image component of button1.*
    Which texture to apply in which sequence can be controlled by numbering the texture files in the ‘Resources’ folder. For further references and understanding, check out this video [Dynamic Level Selection Tutorial][1]

[1]: Unity Mobile Game Tutorial • 2 • Dynamic Level Selection [Tutorial][C#] - YouTube

Any ideas?