The code is very simple, and its returning the value from the debug to console so its working but returning an error I have no idea why.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerLogic : MonoBehaviour
{
public Slider PlayerHealth;
public int Health = 100;
public void Start()
{
PlayerHealth = transform.GetChild(0).GetComponent<Slider>();
Debug.Log(PlayerHealth.value);
}
Also, where is the Slider component located? Because if it is null then it is not located on the very first GameObject under the current GameObject.
If you inst on using GetComponent() and not the inspector the you may want to first do a Find(), for example
if (PlayerHealth == null)
{
GameObject go = GameObject.Find("Player Health"); // looks for a GameObject with the name "Player Health" - you may need to change this
if (go != null)
{
PlayerHealth = go.GetComponent<Slider>();
}
}
if (PlayerHealth != null)
{
Debug.Log(PlayerHealth.value);
}