GetComponents().color = new Color(255f, 0f, 0f, .6f); >}} 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. Thanks
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);
}