C# 当所有9个面板颜色相同时触发布尔
我有一个画布,其中有一个空的游戏对象作为子对象,它又有9个面板,可以改变颜色, 我想在所有9块面板颜色相同时触发一个bool 我尝试获取图像组件,但它显示错误:无法将typeC# 当所有9个面板颜色相同时触发布尔,c#,unity3d,C#,Unity3d,我有一个画布,其中有一个空的游戏对象作为子对象,它又有9个面板,可以改变颜色, 我想在所有9块面板颜色相同时触发一个bool 我尝试获取图像组件,但它显示错误:无法将typeUnityEngine.Color隐式转换为bool 代码如下: void Update() { foreach(Transform child in transform) { if(child.GetComponent<Image>().color = Color.red)
UnityEngine.Color
隐式转换为bool
代码如下:
void Update()
{
foreach(Transform child in transform)
{
if(child.GetComponent<Image>().color = Color.red)
{
Debug.Log("yess");
}
}
}
void Update()
{
foreach(变换中的变换子对象)
{
if(child.GetComponent().color=color.red)
{
Debug.Log(“yess”);
}
}
}
首先,对于要使用的条件,请不要使用=
分配它
其次,当前您正在单独检查每个图像的颜色,但不知道是否所有图像同时与颜色匹配
用你能做的
using System.Linq;
...
// If you can reference these already via the Inspector you can delete the Awake method
[SerializeField] private Image[] images;
// Otherwise get them ONCE on runtime
// avoid repeatedly using GetComponent especially also iterating through many
void Awake ()
{
var imgs = new List<Image>();
foreach(Transform child in transform)
{
imgs.Add(child.GetComponent<Image>();
}
images = imgs.ToArray();
// You probably could also simply use
//images = GetComponentsInChildren<Image>(true);
}
void Update()
{
// This returns true if all images have red color
if(images.All(image => image.color == Color.red))
{
Debug.Log("yess");
}
}
使用System.Linq;
...
//如果您已经可以通过Inspector引用这些方法,则可以删除唤醒方法
[SerializeField]私有映像[]映像;
//否则,在运行时获取它们一次
//避免重复使用GetComponent,尤其是在许多
无效唤醒()
{
var imgs=新列表();
foreach(变换中的变换子对象)
{
Add(child.GetComponent();
}
images=imgs.ToArray();
//你也可以简单地使用
//images=GetComponentsInChildren(true);
}
无效更新()
{
//如果所有图像均为红色,则返回true
if(images.All(image=>image.color==color.red))
{
Debug.Log(“yess”);
}
}