- Home /
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;
}
}
}
}
Answer by robertbu · Mar 11, 2013 at 07:01 AM
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;
}
}
}
}
}
Thanks a lot for your help! I will take a look at your code and try to figure out, what is going on. I know this is very basic stuff. But these are my first steps in coding/scripting.. and I try, to make my own Tic Tac Toe, without copying one of many TTT tutorials out there.
So be prepared .. I will get stuck many more times ;)
Your answer
Follow this Question
Related Questions
OnGUI button created by a foreach loop 4 Answers
Buttons and images 1 Answer
Creating GUI Buttons with a for loop from an array 6 Answers
Do I have to make a GUIStyle with each control?? 1 Answer
Make a Button out of a Textured Plane 2 Answers