dimanche 17 mai 2020

Can Find One Child in Unity but Not the Other

I'm trying to find two different children and update them when certain parameters are met as demonstrated in the code below.

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);

        }
    }

The problem is that the first one UpdateHpBar() works fine but the second one UpdateSupBar() doesn't and I can't find the problem. I've checked the usual problems such as the name being wrong or the child not actually being a child but those aren't the issue. When I run the debugger, it gets to the if-statement fine but then never goes inside, so it must be failing the if-statement. The suppress points are also changing and making a fraction. They're just not showing up on the suppression bar.

I also tried using transform.Find("SuppressionBar") and GetComponentInChildren<Image>().CompareTag("Suppress") but neither of those worked.

I just don't get why the first one works but the second one doesn't.

Aucun commentaire:

Enregistrer un commentaire