How do I get my Buttons to switch colors?
I'm working on a basic Match three in Unity using only the UI. For the objects to match I'm using the UI buttons and having the objects be different colors. For having the objects switch palaces I have a method that switches the color of the buttons. My difficulty is in calling the method upon pressing the buttons. using UnityEngine; using UnityEngine.UI; using System.Collections;
public class match : MonoBehaviour {
public float time = 30.0f;
public float _score;
public GameObject pausePanel;
private Color[] colors = new Color[3];
//public Button[] buttons;
private Image[] image = new Image[16];
public Image im1;
public Image im2;
public Image im3;
public Image im4;
public Image im5;
public Image im6;
public Image im7;
public Image im8;
public Image im9;
public Image im10;
public Image im11;
public Image im12;
public Image im13;
public Image im14;
public Image im15;
public Image im16;
public Image b;
public Image c;
public Image temp;
void Start () {
_score = 0.0f;
colors [0] = Color.red;
colors [1] = Color.blue;
colors [2] = Color.green;
image [0] = im1;
image [1] = im2;
image [2] = im3;
image [3] = im4;
image [4] = im5;
image [5] = im6;
image [6] = im7;
image [7] = im8;
image [8] = im9;
image [9] = im10;
image [10] = im11;
image [11] = im12;
image [12] = im13;
image [13] = im14;
image [14] = im15;
image [15] = im16;
}
void Update () {
time -= Time.deltaTime;
if (time <= 0) {
time = 0.0f;
}
if (time == 0 && _score == 0) {
pausePanel.SetActive (true);
}
AssignColor ();
}
void OnGUI()
{
GUI.Box (new Rect (10, 10, 30, 20), "" + time.ToString ("0"));
GUI.Box (new Rect (85, 410, 30, 20), "" + _score.ToString ("0"));
}
void AssignColor()
{
foreach (Image i in image) {
int colornum = Random.Range (0, colors.Length);
if(i.color == Color.white){
i.color = colors[colornum];
}
}
}
public void getButton(Image i)
{
if (b == null) {
b = i;
}
else if (c == null) {
c = i;
}
if (b != null && c != null) {
Swap(b, c);
b = null;
c = null;
}
}
void Swap(Image ac, Image bc)
{
//Image temp;
temp.color = ac.color;
ac.color = bc.color;
bc.color = temp.color;
}
}
I have all my buttons in my Inspector able to activate getButton, but nothing happens when I press it. my Image b or Image c aren't taking them in.
I managed to get the image b and c to take them in upon pressing them but they still won't switch colors.
You are using AssignColor() in Update. Perhaps it is creating problem . Let me know if it helps
Your answer
Follow this Question
Related Questions
Change button color to intermediate value on onclick() event 1 Answer
UI text wont highlight 0 Answers
How do I change UI button background AND button text on click(pressed)? 0 Answers
How can I change the UI button color when clicked 1 Answer
How to do Image Swap with Text Color the with New UI Button Transition? 0 Answers