Tower Selector Button Challenge

Hi all,

Ben didn’t really give me much to go on with this challenge, not tips on anything really SO I am having trouble and don’t know really what I should be doing. I managed to get the first part of the challenge done by accessing the Sprite Renderer component with this:

        void Start () {
        //black out all buttons

        myColour = new Color(0,0,0,1);
        sr = GetComponent<SpriteRenderer>();
        sr.color = myColour;
	}

That worked, all the buttons are blacked out.

And getting the Button that is clicked upon showing correct colour was done in a similar way:

//catch the OnMouseDown Condition
public void OnMouseDown()
{
    //get the object
    GameObject btnSelected = this.gameObject;

    //make button pressed go to correct colour
    print(name+ ": pressed");
    Color btnColour = new Color(1, 1, 1, 1);
    btnSelected.GetComponent<SpriteRenderer>().color = btnColour;
}//end OnMouseDown

But I cannot figure out how I might go about resetting all the buttons before this so when I choose another buttons they “all” go black to black to start with and then set the one I want with the original colour! The only thing I keep going back to is an array, but, I am am not really up to speed with using arrays in Unity and cycling through the array. So I am really lost. Would someone be able to point me in a direction to take? Hitting my head against the brick wall is starting to hurt :stuck_out_tongue_closed_eyes:.

As always any help is appreciated.

Regards,
Vaughan.

1 Like

Hi Vaughan,

You could consider finding all of the GameObjects of type Button, iterate through the returned collection, and set the colours individually in that loop.

buttons[] = GameObject.FindObjectsOfType<Button>();  // note "objects" not "object"

Don’t forget, we don’t really want to find repetitively, so try to get all of your buttons once and store that reference.

foreach (Button button in buttons)
{
    // ... do things to "button", button.GetComponent for example
}

Hope this helps :slight_smile:

D’oh. Storing them in a array as game objects :man_facepalming:.

Now that you’ve broken it down like that it seems simple! I’m sure I’ll make a complete schmozzle of it though, but, you have given me information that I can act upon.

The foreach loops in Unity always thrown me for a loop (pun intended!?!). I always preferred do/while for/next in other programming languages.

An exciting challenge though to be sure - although I reserve the right to “failure is always an option” :smiley:.

THANKYOU!!!

1 Like

You’re welcome Vaughan :slight_smile:

And it’s done- yay!!! I bet Ben’s solution is a hell of a lot better than mine though.

Took a little while, had a visit to the doctor and physiotherapist telling me that my shoulder might not heal properly for up to 2 years :scream::scream::scream:

Still having a mild mind meltdown with that news and the arrays and “foreach”, but, getting a little better.

Thanks again @Rob.

1 Like

Ooof, sorry to hear about the shoulder Vaughan, make sure you take it steady and follow any advice they have given. Fingers crossed for a quicker recovery.

With regards to arrays and “foreach”, you could always create a separate project / scene and just experiment a little with those away from the main project. You may find it “clicks” when it is based around something you have put together yourself.

:slight_smile:

Taking your advice and working on it!

Cheers.

1 Like

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

Privacy & Terms