I want to change color of one button only!

this is what I made,

LAST working code looks like this and I came back on it again…

using UnityEngine;
using System.Collections;

public class MakeGame: MonoBehaviour 
{
	private Rect windowRect;
	public GameObject PlanePrefab;
    private int buttonWidth = 100;
	private int buttonHeight = 50;	
	public Texture btnWT;
	public Texture btnRT;
	public Texture btnT;
	private float btnsize;
	
void Start () 
	{
	btnT = btnWT;
	windowRect = new Rect( 0, 0, Screen.width, Screen.height);
	btnsize = 20f;
	}
	
	
void Update () 
	{
	
	}
	
void OnGUI() 	
	{
	
		
		if (GUI.Button(new Rect(Screen.width / 2 + 150, buttonHeight / 2, buttonWidth, buttonHeight), "Mine Menu")) 
			{
				Application.LoadLevel(0);
		    }
		if (GUI.Button(new Rect(Screen.width / 2 + 150, buttonHeight / 2 + (10 + buttonHeight)*1, buttonWidth, buttonHeight), "Save Map")) 
			{
				Debug.Log("Does nothing");
		    }
		if (GUI.Button(new Rect(Screen.width / 2 + 150, buttonHeight / 2 + (10 + buttonHeight)*2, buttonWidth, buttonHeight), "Load Map")) 
			{
				Debug.Log("Does nothing");
		    }
		
	for (int k = 1; k <= 32; k++)
	{
	for (int l = 1; l <= 32; l++)
		{
		if (GUI.Button(new Rect(50 + btnsize * k, 25+ btnsize * l, btnsize, btnsize), btnT)) 
			{
				
				if (btnT == btnWT) 
					{
						btnT = btnRT;
						Debug.Log("white to red");
					}
				else 
					{
						btnT = btnWT;
						Debug.Log("red to white");
					}
		    }
		}
	}
	}
}//PlayerPrefs

When I click on any button ALL buttons change image to red
I want to change color of clicked button ONLY! :frowning:

I tryed to array texture but it doesn’t want to read texture index like “btnT[((k-1)*32)+l]” (that INDEX code is used in BOTH FOR LOOPS!)

One thing I noticed is that you’re using an int for some of your GUI elements. They need to be floats. That could be causing several issues.

If I understand correctly, you want to click one of the buttons to toggle its colour. Keep in mind that OnGUI() executes every frame. That means when you change btnT, it will change for all of your textures.

A better way to do it would be to have a two-dimensional array that tracks what colour each button is. Try something like this: (WARNING: untested code)

public bool [,] MapArray = new bool [];

for your map declaration. Then in your loop, do

public Texture RedTexture;
public Texture WhiteTexture;

for (int K = 0, K <= MapArray.GetUpperBound(0); K++) {

   for (int L = 0; L <= MapArray.GetUpperBound(1); L++) {
      if (GUI.Button(new Rect(WhateverYourRectVarIs, MapArray[K,L] ? RedTexture : WhiteTexture))) {
         MapArray[K,L] = !MapArray[K,L];
      }
   }

}

Here, I’ve defined that a “false” value is a white button, and a “true” value is red. You could easily swap those, and it will probably work fine. I’m also using a shortcut construct that works like this:

? : ;

Not sure if this construct saves processor cycles, but it does tend to save keystrokes.

The advantage of using an array is that you can easily serialise it to save it to disk, and you can iterate through it easily like we’ve done here. I’ve always found that the best way to accomplish things like this is to always have an internal “authoritative” data structure, and just have your GUI parrot it off. It saves a lot of headaches. Also, having an array allows you to easily resize your map later if you want to. Make sure to set the array’s size in your inspector before running it.

Hope that helps!

y thank you this explains something,
I like using arrays, I tried to make it work, but it didn’t, I posted an thread before this called (How to “talk” to objects) it is failed title, I edited it after I made it in ARRAY TEXTURES, I have posted whole code that I made with array variables, so if you like check it out you can.
Btw that is the only way how I KNOW “how to use” arrays in this problem (by knowing I mean I know how to use it logically not just copy paste without understanding. That my code looks nice to me but I did something wrong because I don’t know conditions that C# needs for using arrays in GUI or for loops) I’ll try to figure out your code. I haven’t got it yet, but it looks nice and I think I’ll get it (compleately understand how it works) after some testing and playing with it.