0

I'm trying to assign colors to a array of buttons randomly using an array of colors, but it doesn't work. I seem to be getting a NullReferenceException for my foreachloop but I don't know why that is. For those who wish to know I'm trying to make a simple match three game for an assignment and I have to use UI, I can't use GameObjects, and I want to use the different color buttons for the objects in the game.

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;
  //public Button[] buttons;
  private Image[] image;
  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;

  // Use this for initialization
  void Start () {
    _score = 0.0f;
    colors = new Color[3];
    //buttons = new Button[16];
    image = new Image[16];
    //colors [0] = Color.red;
    //colors [1] = Color.blue;
    //colors [2] = Color.green;
    colors [0] = GetComponent<UnityEngine.UI.Image>().color = Color.red;
    colors [1] = GetComponent<UnityEngine.UI.Image>().color = Color.blue;
    colors [2] = GetComponent<UnityEngine.UI.Image>().color = 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;

}

// Update is called once per frame
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()
{
    Debug.Log("Sup");
    int colornum = Random.Range (0, colors.Length);
    foreach (Image i in image) {
        if(i.color == Color.white){
            i.color = colors[colornum];
        }
    }

}
Jake Howard
  • 21
  • 1
  • 8
  • Did you assign a value to every `im*` field? If any of them is null that foreach loop will still set `i` to it at some point. (By the way, you should be able to use arrays in Unity without that repetitive workaround.) – 31eee384 Sep 25 '15 at 20:16
  • By assign a value to every im* field, do you mean assigning them in the inspector of unity? because i did that. – Jake Howard Sep 25 '15 at 20:20
  • That's what I mean, yes. Well, I suggest using a debugger, or if you can't get that working, `Debug.Log` until you have a more detailed question. (For example, log if `i` is null, if `colors[colornum]` is null, etc. General debugging stuff.) – 31eee384 Sep 25 '15 at 20:23
  • Okay I did do the log thing you suggested and it is returning that i is null though I don't know why since my array does have values in it. – Jake Howard Sep 25 '15 at 20:34
  • Okay I made some changes to how color works I removed the getComponents stuff and simply stated that colors[0] = Color.Red and so on. The NullReferenceException is gone though all the buttons are becoming the same color. I'll try to solve it myself but feel free to leave a suggestion. – Jake Howard Sep 25 '15 at 21:16
  • quick fix just had to move the colornum declaration into the loop – Jake Howard Sep 25 '15 at 21:50

0 Answers0