"Button.buttonArray is never assigned to..." error message

My buttons do not change to black when mouse click occurs, the problem begins with the array not working. The code is as tutorial. The error i get when trying to build the script is:

image

public class Button : MonoBehaviour {

	private Button[] buttonArray;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void OnMouseDown (){
		print (name + " pressed");
		// loop through buttons in the Array
		foreach (Button thisButton in buttonArray) {
			thisButton.GetComponent<SpriteRenderer> ().color = Color.black;
		}
		GetComponent<SpriteRenderer> ().color = Color.white; 
	}

Hi Andy,

You don’t have anything initialising that array, you’ll need something like this in your Start method;

private void Start()
{
    buttonArray = GameObject.FindObjectsOfType<Button>();
}

Hope this helps :slight_smile:

Wood for the trees. Thanks Rob, that got rid of the error. I still don’t get the expected behaviour though. When i click, I get the “white” button, but when I click another, the previous remains white.

For the buttons to go back to black, I’ve had to add the OnMouseExit method, which looks a little pants, but it does the job. Which of course means, the array isn’t actually doing anything?

1 Like

Hi Andy,

You’re welcome.

I’ve just tested this same code from the completed project and it appears to work.

Just to check;

  • you have the Buttons GameObject in the Hierarchy and this has a Canvas component
  • the defenders used as buttons within this each have a Button.cs script, a BoxCollider2D with Is Trigger enabled
  • each SpriteRenderer component on each of the defender buttons has it’s colour set to black as default

Hi Rob, Thanks for checking. So this is the “background”, which is childed only, to the Buttons GameObject, so, its not set as a component to the Buttons gameobject, as such, but then, that didn’t happen in the videos/tut, it was just dragged a child.

Then below is the actual button, each with a trigger set.

As an aside to this, I added a print into the array to see what was happening, and sure enough, if I click on a button, then 4 names get displayed. i.e. click on stone once, and I get:

image

So the array is doing something, it just doesnt seem to be setting each objects colour.

foreach (Button thisButton in buttonArray) {
		GetComponent<SpriteRenderer> ().color = Color.black;
		print (name);
	}

Hi Andy,

Here’s the Hierarchy I have on the completed project;

image

The print statement is a good idea, and yes, doing what we would expect.

Your code though isn’t quite right in that foreach statement, ironically it was in your first screenshot at the top of this topic.

You want;

thisButton.GetComponent<SpriteRenderer>().color = Color.black;

The above will reference the specific button in the foreach iteration.

    /// <summary>
    /// Handles the MouseDown event for the defender buttons
    /// </summary>
    void OnMouseDown()
    {
        foreach (Button thisButton in buttonArray)
        {
            thisButton.GetComponent<SpriteRenderer>().color = Color.black;
        }

        gameObject.GetComponent<SpriteRenderer>().color = Color.white;

        selectedDefender = defenderPrefab;  // ignore this line, you'll probably come to this in a bit
    }

Rob, as you were typing, i spotted the error! My word, its been a long day :slight_smile:

Thanks for your efforts Rob, “thisButton.” was the problem.

1 Like

Fab, glad you have it resolved and can move forwards :slight_smile:

Haha yes, I already did, i slept on this one and thought about it while at work today! :blush:

1 Like

…that’s what work is for! :smiley:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms