- Home /
Change color of all buttons listed in array
Hi, I am trying to change the colour of all buttons listed in an array, however I can't seem to figure it out.
This does not throw up any errors but the colour component of the buttons tagged "buttonBall" does not change. Can anybody see where I am going wrong? any help would be appreciated. Thankspublic GameObject[] buttonArray;
void Update() {
buttonArray = GameObject.FindGameObjectsWithTag("buttonBall");
for (int i = 25; i < buttonArray.Length; i++) {
GetComponents()[i].color = new Color(255f, 0f, 0f, .6f);
}}
Answer by Piflik · Apr 29, 2016 at 01:29 PM
This changes the actual buttons' colors (the normal color, not highlighted, pressed or disabled).
using System.Linq;
//attention: GameObject.Find... are really slow functions and should be called as seldom as possible
Button[] buttonArray = GameObject.FindGameObjectsWithTag("buttonBall").Select(obj => obj.GetComponent<Button>()).ToArray();
foreach(Button button in buttonArray) {
button.colors.normalColor = new Color(255, 0, 0, 0.6f);
}
If you want to change the color of the image component, then you have to edit the code accordingly.
using System.Linq;
//attention: GameObject.Find... are really slow functions and should be called as seldom as possible
Image[] buttonArray = GameObject.FindGameObjectsWithTag("buttonBall").Select(obj => obj.GetComponent<Image>()).ToArray();
foreach(Image img in buttonArray) {
img.color = new Color(255, 0, 0, 0.6f);
}
If you don't want to use Linq, you can do this instead:
GameObject[] objs = GameObject.FindGameObjectsWithTag("buttonBall");
foreach(gameObject go in objs) {
go.GetComponent<Image>().color = new Color(255, 0, 0, 0.6f);
}
All of the above assume, that every object that is tagged 'buttonBall' has a Button or Image Component. If that is not the case:
GameObject[] objs = GameObject.FindGameObjectsWithTag("buttonBall");
foreach(gameObject go in objs) {
Image img = go.GetComponent<Image>()
if (img != null) img.color = new Color(255, 0, 0, 0.6f);
}
Answer by jgodfrey · Apr 28, 2016 at 08:50 PM
Why does your for loop start at 25? That looks problematic. If you're really trying to change all items in the array, that should be:
for (int i = 0; ...
Thanks for you help. The for loop is working fine now as I can "Destroy" all button on request, however the colour change script throws up the error; "Array index is out of range..." Cant wrap my head around why this is. Any ideas on a solutions?
Thanks
Answer by rodude123 · Apr 28, 2016 at 08:56 PM
Here this should fix the problem.
public GameObject[] buttonArray;
void Update()
{
buttonArray = GameObject.FindGameObjectsWithTag("buttonBall");
for (int i = 1; i < buttonArray.Length; i++)
{
buttonArray[i].color = new Color(255f, 0f, 0f, .6f);
}
}
This won't work if the case only has one button. And for dynamic results, we should assume that it could be.
for (int i = 1; i < buttonArray.Length; i++)
should be:
for (int i = 1; i <= buttonArray.Length; i++)
or:
for (int i = 0; i < buttonArray.Length; i++)
To safely process all elements, it has to be the last one.
The first one will miss the first element
The second one will miss the first element and try to access an element at the end that doesn't exist
The third one will process all elements correctly
Which is exactly what my original answer addresses.
Thanks for your suggestion, however this brings up the error; "`UnityEngine.GameObject' does not contain a definition for `color' ..."
I changed the script slightly to go through Image;
buttonArray[i].GetComponents()[i].color = new Color(255f, 0f, 0f, .6f);
Though this throws up the error; "Array index is out of range..."
Your answer
Follow this Question
Related Questions
How to add a gray scale to a buttons color? 0 Answers
Multiple Cars not working 1 Answer
Help with Playerprefs to saving colors of buttons. 0 Answers
Distribute terrain in zones 3 Answers