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!