How to loop through Texture array and add textures to buttons in window?

Please check the comments in the code to find where it is working and where it is not. I am needing to loop through an array of Texture objects and assign each texture to a button in a window.

	void OnGUI()
	{
		GUI.color = Color.gray;
		windowRect = GUI.Window(1, windowRect, AddWindowControls, "Terrain Textures");
	}
	
	void AddWindowControls(int windowId)
	{
		//This works but is not what I want.
		GUI.Button(new Rect(0, 0, 100, 100), TerrainTextures[0]);
		GUI.Button(new Rect(100, 0, 100, 100), TerrainTextures[1]);
		
		//TerrainTextures is an array of Texture objects.		
		if(!TerrainTexturesLoaded)
		{
			foreach(Texture thisTexture in TerrainTextures)
			{
				//This does not work...
				GUI.Button(new Rect(0, 0, 100, 100), thisTexture);
				//Debug.Log(thisTexture.name.ToString());
			}
                 }

How about doing it more like this:

if(!TerrainTexturesLoaded)
{
      for(int i = 0; i < TerrainTextures.Length; i++) //go through all textures
      {
          GUI.Button(new Rect(0, 100 * i, 100, 100), TerrainTextures[i]); //use i to offset the y-position
      }
}

That works, but I really don’t understand why it would not work the other way. Thank you for the help though.


EDIT: I forgot to comment out the two lines that were working when I ran the project initially. This is actually not working either.


EDIT: I have now changed the code to the following:

The Debug.Log method is being executed the correct number of times and also returning the correct expected data. Yet the buttons still do not appear…


For anyone who might be keeping score, I finally got it to work by removing the code from the if statement. This is so odd, though. The if statement was executing properly as the Debug.Log method calls indicate. Perhaps the GUI.Button calls were somehow out of scope when nested two layers deep?

Grrrrr…

It’s a strange problem. It may help to use the debugger (with breakpoints) so you can and really take a close look at that data, rather than relying on Debug.Log statements.