How does foreach loop know what a "button" is?

  private void OnMouseDown()
    {
        var buttons = FindObjectsOfType<DefenderButton>();
        foreach(DefenderButton button in buttons) 
        {
            button.GetComponent<SpriteRenderer>().color = new Color32(41,41,41,255);
        }
        GetComponent<SpriteRenderer>().color = Color.white;   
    }

We define any objects with the DefenderButton script as a “buttons” variable and then use a foreach loop to say each “button” inside a “buttons” in the “buttons” array we will do {this line of cod}

But how does it know what a “Button” is? We never defined or labeled these buttons as buttons (or at least didn’t) and it still works anyway.
I did not change the name of my button’s either and they still work without having to tag or label them as a “button”

I must be missing something here… And why do we state the defender class script before writing button in buttons?

Good questions.

Ironically, you’ve kind of answered your first question with the second question.

Remember that to declare a variable, you need two things. A type and a name. That’s what we have here. DefenderButton is a type and “button” is a name. So you’ve made a variable called button of type DefenderButton.

“var” kind of hides this information but FindObjectsOfType (notice that it’s Objects, not Object) returns an array, and this array includes all the items found. The array is of type DefenderButton, so all the objects inside “buttons” are of type DefenderButton.

As the loop iterates over each DefenderButton in buttons, it’s putting that into “button”, a variable that holds a DefenderButton.

Because that variable was declared in the local scope of the foreach loop, it only exists in that foreach loop. So that variable holds a reference to the current item being iterated over in the array (because that reference was copied into the “button” variable. Each time it goes through the loop, it creates the variable again and populates it by the next item “in buttons”.

So the answer to question #1 is “you defined the variable “button” in the parenthesis while seeting of the for loop. This works a lot like how you create temporary variables in a method when you setup the parameter in parenthesis.”

The answer to question #2 is “you specify DefenderButton because that’s the first part of declaring a variable. You are working with an array of DefenderButtons so your variable has to be a DefenderButton. You could have also said ‘var’ instead and the compiler would have figured it out. You can see this done above where the variable ‘buttons’ is declared.”

1 Like

But you did. Right here:
var buttons = FindObjectsOfType<DefenderButton>();

Wait I think I solved it.

when we write within the parenthesis of the foreach we are writing the data type of the new variable we are matching with the collection which must be the same type.

we can also just write var so that it can assume the var type from the collection.

so
foreach(var rod in mybottle)
{
instantiate (rod, transform.position , quaternian.identity);
}

here we labeled all the things that are catagorized as part of the mybottle array as a rod variable. and told it to make every rod to come into position

Yes. Sorry. I saw you typing and realized all of this a minute before you answered

Sometimes it takes time for the penny to drop.

At least you worked it out.

1 Like

it took an obscure youtube video to figure it out. Unfortunely in the old version of the course at least, Rick has the tendancy to label things a bit too similarly to each other…

button for singular, buttons for multiple in an array/list/collection is pretty standard.

You’ll get used to it. It’s how you’re supposed to do it. When you’re reading other people’s code or having others help you with your code, keeping to coding practises helps others understand the intention of the code. I’ve seen many people ask for help and their code is so difficult to read, they don’t get any help or they get the wrong advice as others misunderstand their code. I’ve seen people name classes with camel case and variables with pascal case. It just sucks too much unnecessary energy out of you to read the code so they don’t get help they desperately need.

1 Like

That all being said, GameDev.tv doesn’t teach in a formal methology when teaching new c# concepts, they teach you through exposure and an occasional a quick explanation. I don’t necessarily like this way, but it’s the way they’ve gone.

1 Like

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

Privacy & Terms