Why no for each loop over Button for setting starCost

Why it is’nt necessary to use a loop to set the starCost for each defender Prefab in the Start() routine but it is necessary in the OnMouseDown() one? In the case of starCost, a kind of iteration is needed because there are 4 text elements to write. In the case of OnMouseDown, there is only a single element selected by the mouse so why there is a need for an iteration?

I’m not 100% sure if I understand your question but I think I do and here’s a possible answer:

The script we are talking about is the Button.cs script, which is on every Button in the scene. Every Button also has its own child, being the starText it wants to set, and its own linked defender prefab. Because this script is on every individual Button, the script only needs to find its own Child’s Text component and it’s own linked defender prefab’s starCost.

The foreach loop used on the OnMouseDown() function only sets all buttons (including the one you clicked) to a choosen color (in this case black), after which it changes the clicked one to white.

So in other words: the button scripts executes 4 times: once for every button - every button finds its own child’s Text component and its own linked defenderPrefabs starCost value. Anytime you click a button, which activates that specific buttons OnMouseDown() function, it needs to set all buttons to black out of that clicked Buttons script.

I hope this makes sense to you and I answered your question.