When i try to use transform.FindChild("Content").GetComponent(); it doesn't found it

I have a script attached to an image that i’m using to make an healthBar, like the IMG.


And This is my script

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class BarScript : MonoBehaviour {

    [SerializeField]
    private float fillamount;

    [SerializeField]
    private Image content;


    public Image container;

    [SerializeField]
    private Text valueText;

    [SerializeField]
    private float lerpSpeed;

    public float MaxValue { get; set; }
    
    public float Value
    {
        set
        {
            string[] tmp = valueText.text.Split('/');
            valueText.text = value + " / " + MaxValue;
            fillamount = map(value, 0, MaxValue, 0, 1);
        }
    }
     
    public void Start ()
    {
        container = transform.FindChild("Content").GetComponent<Image>();

The live above is nº37

        //FindChild("Content").
    }

    void Update ()
    {
        handleBar();
	}

    private void handleBar()
    {
        if (fillamount != content.fillAmount)
        {
            content.fillAmount = Mathf.Lerp(content.fillAmount,fillamount,Time.deltaTime * lerpSpeed) ;
        }
        
    }

    private float map(float value, float inMin, float inMax, float outMin, float outMax)
    {
        return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
    }
}

when i run the game he doens’t get the “content”, and this is the error that appears

“NullReferenceException: Object reference not set to an instance of an object
BarScript.Start () (at Assets/Script/BarScript.cs:37)”

Does someone knows why the ‘transform.FindChild(“Content”).GetComponent();’ isn’t working?

Thank You!

Use this modified version of your code to fix your problem

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class BarScript : MonoBehaviour
{
    [SerializeField]
    private float fillamount;

    [SerializeField]
    private Image content;


    public Image container;

    [SerializeField]
    private Text valueText;

    [SerializeField]
    private float lerpSpeed;

    public float MaxValue { get; set; }

    public float Value
    {
        set
        {
            string[] tmp = valueText.text.Split('/');
            valueText.text = value + " / " + MaxValue;
            fillamount = map(value, 0, MaxValue, 0, 1);
        }
    }

    void Start ()
    {
        // content is what is currently used in this script not container
        if (content == null) // its a public so it can be assigned in the inspector
        {
            Transform t = FindChild ("Content", transform);
            if (t != null)
            {
                content = t.gameObject.GetComponent<Image>();
            }
        }
    }

    Transform FindChild (string name, Transform t)
    {
        if (t.name == name)
            return t;

        foreach (Transform child in t)
        {
            Transform ct = FindChild (name, child);
            if (ct != null)
                return ct;
        }
        return null;
    }

    void Update ()
    {
        handleBar();
    }

    private void handleBar()
    {
        if ((content != null) && (fillamount != content.fillAmount))
        {
            content.fillAmount = Mathf.Lerp(content.fillAmount,fillamount,Time.deltaTime * lerpSpeed) ;
        }
    }

    private float map(float value, float inMin, float inMax, float outMin, float outMax)
    {
        return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
    }
}