change texture of GuiButton in for loop

Hello,

I´m very new to programing and started to learn c# a couple of weeks ago.
I want to create a Tic Tac Toe game in Unity. So my first goal is to create a functional 3x3 board.

I use a 2d array and nested for loops to create the board. Now I´m trying to find a way, how to change the texture of one button when clicked. I´ve set up an array of textures, so I can use the indexes for each texture (X,O and blank).

So, my problem is… Because the Buttons are drawn in the for loop, when I change the index via mouseclick, every Button gets the same texture. I need a way to assign the texture after the for loop. Can you help me with this?

Thanks,

Manuel

using UnityEngine;
using System.Collections;

public class buttons : MonoBehaviour {
	
//Variables////////////////////////////////////////////////////////
//
	
	public GUISkin customSkin;
	
	public Texture2D[] buttonTex;
	
	int playerX = 	1;
	int playerO = 	2;
	int blank	=  	0;

	
	int turn = -1;
	int state = 0;
	
	public int x;
	public int y;
	


	
//Game Board///////////////////////////////////////////////////////////////////////////////////
	//
	
	void OnGUI (){
		
	//Custom Skin
		//
		
	GUI.skin = customSkin;
		
		

		// Draw 3x3 Board
		//
		

		
	int index = 0;
		
	int [,] board = new int [3,3];
		
	for (y = 0; y < 3; y++)
	{
	for (x = 0; x < 3; x++)
		{
			if (GUI.Button (new Rect (x *100,y *100, 100, 100), new GUIContent (buttonTex[state + 1])))
					
			{
				
				transform.localScale = new Vector3(0.1F, 0, 0);
				state = turn;
				turn = turn * -1;
				
				
			}	
		}

	}			
		
}

You need to keep track of the state of each button individually. Here are a few changes to your code. I’m not sure if this is what you were going to use board[,] for.

using UnityEngine;
using System.Collections;
 
public class buttons : MonoBehaviour {
 
//Variables////////////////////////////////////////////////////////
//
    public GUISkin customSkin;
 
    public Texture2D[] buttonTex;
 
    int playerX =  1;
    int playerO =  2;
    int blank  =    0;
 
 
    int turn = -1;
    int state = 0;
 
    public int x;
    public int y;
	
	private int [,] board = new int [3,3] {{0,0,0},{0,0,0},{0,0,0}};
 
//Game Board///////////////////////////////////////////////////////////////////////////////////
    //
 
    void OnGUI (){
 
    //Custom Skin
       //
 
    GUI.skin = customSkin;
 
       // Draw 3x3 Board
       //
 
    int index = 0;

    for (y = 0; y < 3; y++)
    {
    for (x = 0; x < 3; x++)
       {
         if (GUI.Button (new Rect (x *100,y *100, 100, 100), new GUIContent (buttonTex[board[y,x]])))
 
         {
          //transform.localScale = new Vector3(0.1F, 0, 0);
          //state = turn;
          //turn = turn * -1;
			board[y,x] = (board[y,x] + 1) % 3;
         }    
       }
 
    }       
}
	
}