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
 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!
Answer by TBruce · Aug 28, 2016 at 05:13 PM
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;
     }
 }
Thank You tho i already have found another solution that was:
transform.FindChild("$$anonymous$$ask").FindChild("Content").getcomponent();
Your answer
 
 
             Follow this Question
Related Questions
A Problem With A Script Of GUI.HorizontalSlider It Do not Move? 0 Answers
I have a problem with this script and an error appeared, does anyone know why? 0 Answers
How to reference this game object to another class 0 Answers
Can someone tell me what is wrong with this script? 1 Answer
in my script component it says the compile error has to be fixed i dont know how to do that 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                