可以在 Unity 中找到一个 Child,但找不到另一个
Can Find One Child in Unity but Not the Other
我试图找到两个不同的 children 并在满足某些参数时更新它们,如下面的代码所示。
private void UpdateHpBar()
{
if (GetComponentInChildren<Image>() != null && GetComponentInChildren<Image>().name == "HealthBar")
{
GetComponentInChildren<Image>().transform.localScale = new Vector3((float)((float)HitPoints / (float)TotalHitPoints), 1, 1);
GetComponentInChildren<Image>().color = Color.Lerp(Color.red, Color.green,
(float)((float)HitPoints / (float)TotalHitPoints));
}
}
public void UpdateSupBar()
{
if (GetComponentInChildren<Image>() != null && GetComponentInChildren<Image>().name == "SuppressionBar")
{
GetComponentInChildren<Image>().transform.localScale = new Vector3((float)((float)SuppressPoints / (float)TotalSuppressPoints), 1, 1);
}
}
问题是第一个 UpdateHpBar() 工作正常,但第二个 UpdateSupBar() 没有,我找不到问题所在。我已经检查了常见的问题,例如名称错误或 child 实际上不是 child 但这些都不是问题。当我 运行 调试器时,它进入 if-statement 正常,但之后永远不会进入内部,所以它一定是 if-statement 失败了。压制点也在变化,并成为一小部分。他们只是没有出现在抑制栏上。
我也试过使用transform.Find("SuppressionBar")
和
GetComponentInChildren<Image>().CompareTag("Suppress")
但这些都没有用。
我只是不明白为什么第一个有效而第二个无效。
GetComponentInChildren returns 您指定的类型的第一个组件,所以如果它首先找到 HpBar 的图像组件,它总是会是这样。
让您的代码更好地工作的简单统一方法是简单地为每个代码添加一个硬引用。
[SerializeField] private Image healtBar;
[SerializeField] private Image SuppresionBar;
private void UpdateHpBar()
{
healtBar.transform.localScale = new Vector3((float)((float)HitPoints / (float)TotalHitPoints), 1, 1);
healtBar.color = Color.Lerp(Color.red, Color.green,
(float)((float)HitPoints / (float)TotalHitPoints));
}
public void UpdateSupBar()
{
SuppresionBar.transform.localScale = new Vector3((float)((float)SuppressPoints / (float)TotalSuppressPoints), 1, 1);
}
上面声明的 serializefields 将在您的脚本中显示在 unity 中。当不在播放模式时,将你的 hpbar 和抑制条拖放到字段上 link 它们。
我试图找到两个不同的 children 并在满足某些参数时更新它们,如下面的代码所示。
private void UpdateHpBar()
{
if (GetComponentInChildren<Image>() != null && GetComponentInChildren<Image>().name == "HealthBar")
{
GetComponentInChildren<Image>().transform.localScale = new Vector3((float)((float)HitPoints / (float)TotalHitPoints), 1, 1);
GetComponentInChildren<Image>().color = Color.Lerp(Color.red, Color.green,
(float)((float)HitPoints / (float)TotalHitPoints));
}
}
public void UpdateSupBar()
{
if (GetComponentInChildren<Image>() != null && GetComponentInChildren<Image>().name == "SuppressionBar")
{
GetComponentInChildren<Image>().transform.localScale = new Vector3((float)((float)SuppressPoints / (float)TotalSuppressPoints), 1, 1);
}
}
问题是第一个 UpdateHpBar() 工作正常,但第二个 UpdateSupBar() 没有,我找不到问题所在。我已经检查了常见的问题,例如名称错误或 child 实际上不是 child 但这些都不是问题。当我 运行 调试器时,它进入 if-statement 正常,但之后永远不会进入内部,所以它一定是 if-statement 失败了。压制点也在变化,并成为一小部分。他们只是没有出现在抑制栏上。
我也试过使用transform.Find("SuppressionBar")
和
GetComponentInChildren<Image>().CompareTag("Suppress")
但这些都没有用。
我只是不明白为什么第一个有效而第二个无效。
GetComponentInChildren returns 您指定的类型的第一个组件,所以如果它首先找到 HpBar 的图像组件,它总是会是这样。
让您的代码更好地工作的简单统一方法是简单地为每个代码添加一个硬引用。
[SerializeField] private Image healtBar;
[SerializeField] private Image SuppresionBar;
private void UpdateHpBar()
{
healtBar.transform.localScale = new Vector3((float)((float)HitPoints / (float)TotalHitPoints), 1, 1);
healtBar.color = Color.Lerp(Color.red, Color.green,
(float)((float)HitPoints / (float)TotalHitPoints));
}
public void UpdateSupBar()
{
SuppresionBar.transform.localScale = new Vector3((float)((float)SuppressPoints / (float)TotalSuppressPoints), 1, 1);
}
上面声明的 serializefields 将在您的脚本中显示在 unity 中。当不在播放模式时,将你的 hpbar 和抑制条拖放到字段上 link 它们。