- Home /
How to get all components that contain Color field
Let's say while working with UI I have 2 Images and 3 Texts and I'd like to change color on them, can I somehow have reference to them in one variable that I can iterate on and change color for each? so instead of
foreach (var image in images)
{
image.color = Color.red;
}
foreach (var text in texts)
{
text.color = Color.red;
}
I can do something like
foreach (var component in componentsWithColor)
{
component.color = Color.red;
}
Answer by Vega4Life · Nov 27, 2018 at 05:01 PM
I think the best way for you to get what you want without being to complicated is create container classes for your images and texts. These classes will derive from a base class of 'ComponentWithColor'. This class will have an abstract method to change the color - which both container classes must create.
Since both containers derive from the base class, they can be in a list and call the same method to change their color. Here is a quick example of how this works:
using UnityEngine;
// Our base abstract class for things wanting to change their color
public abstract class ComponentWithColor : MonoBehaviour
{
public abstract void ChangeColor(Color color);
}
using UnityEngine;
// Put this on whatever you are using for images and link it up
public class ImageContainer : ComponentWithColor
{
// I am just using a sprite for demo purposes
[SerializeField] SpriteRenderer image;
public override void ChangeColor(Color color)
{
image.color = color;
}
}
using UnityEngine;
using UnityEngine.UI;
// Add this to the text you are using and link it
public class TexContainer: ComponentWithColor
{
// Basic text for demo purposes
[SerializeField] Text text;
public override void ChangeColor(Color color)
{
text.color = color;
}
}
using System.Collections.Generic;
using UnityEngine;
// Just a demo way to show how they change color - add to your scene somewhere and link your containers
public class ColorManager : MonoBehaviour
{
// I just linked the containers to this list (both containers were on objects in the scene)
[SerializeField] List<ComponentWithColor> colorComponents;
void Update()
{
// Just hit space bar to change their color
if (Input.GetKeyDown(KeyCode.Space))
{
foreach(ComponentWithColor component in colorComponents)
{
component.ChangeColor(Color.red);
}
}
}
}
Overall, it's a decent way to handle what you want. If I were going to create something like this, I would handle it with an interface, then add it to both containers (instead of a base class). The problem though with this solution is you can't link interfaces in the inspector... without creating a custom one.
Answer by Tigewulf · Jun 16, 2021 at 07:42 AM
I just had a similar problem.
You can use MaskableGraphic
e.g. GetComponentsInChildren<MaskableGraphic>()
Your answer
Follow this Question
Related Questions
Color changer using HEX in game 1 Answer
How to change a buttons color? 2 Answers
UI text color not assigning correctly 2 Answers
Multiple Cars not working 1 Answer
UI Button color 1 Answer