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

public class HealthBar : MonoBehaviour
{
    private Lifebar _Player1Lifebar = null;
    private Lifebar _Player2Lifebar = null;

    public Slider Player1Slider = null;
    public Slider Player2Slider = null;

    void Start()
    {
        GameObject player1 = GameObject.Find("Player1");
        GameObject player2 = GameObject.Find("Player2");

        if (player1 != null)
        {
            _Player1Lifebar = (Lifebar) player1.GetComponent(typeof(Lifebar));
        }
        else
        {
            Debug.LogError("Player1 doesn' t have a Lifebar Script");
        }

        if (player2 != null)
        {
            _Player2Lifebar = (Lifebar)player2.GetComponent(typeof(Lifebar));
        }
        else
        {
            Debug.LogError("Player2 doesn' t have a Lifebar Script");
        }

        if (Player1Slider == null)
        {
            Player1Slider = (Slider)transform.FindChild("Lifebar1").GetComponent(typeof(Slider));
        }

        if (Player2Slider == null)
        {
            Player2Slider = (Slider)transform.FindChild("Lifebar2").GetComponent(typeof(Slider));
        }

    }

    void Update()
    {
        if (_Player1Lifebar != null)
        {
            if (Player1Slider != null)
            {
                Player1Slider.value = _Player1Lifebar.Life;
            }
        }

        if (_Player2Lifebar != null)
        {
            if (Player2Slider != null)
            {
                Player2Slider.value = _Player2Lifebar.Life;
            }
        }
    }
}
