I wanted to add a healthbar and use localscale to represent how much health the player has in this code but I am unsure in which script to put it in. The health script is accessed by the player and the enemy. Still fairly new to coding so forgive me for any errors.
This is my health code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class HealthSystem : MonoBehaviour
{
public int startingHealth = 100;
public bool isOnEnemyList = false;
public GameObject healthBar;
private int health = 0;
public int Health
{
set
{
//Set the health value for each character that has this script attached
health = value;
}
get
{
//Return a value if it has changed.
return health;
}
}
void Awake()
{
//Set the max health as the current health at the beginning
health = startingHealth;
}
void Update()
{
//If the health goes below 0 and is on the enemy list, destroy that object.
if (health <= 0)
{
if (isOnEnemyList) WeaponFire.enemyList.Remove(gameObject);
Destroy(gameObject);
Debug.Log(gameObject.name + ": I'M DYING OFF NOW!");
}
}
}
This is the damageplayer script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AIBehaviour : MonoBehaviour
{
private HealthSystem thePlayer = null;
GameObject playerObject;
void Start()
{
playerObject = GameObject.FindGameObjectWithTag("Player");
if (playerObject == null)
Debug.Log("Couldn't find player tag.");
thePlayer = playerObject.GetComponent<HealthSystem>();
if (thePlayer == null)
Debug.Log("Couldn't find health system");
//thePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent<HealthSystem>();
}
void Update()
{
//Lets us know if the player HP has reached zero.
if (thePlayer.Health <= 0)
{
Debug.Log("You Lose");
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
TakeDamage();
//thePlayer.Health -= 10;
Debug.Log("Player is losing health" + thePlayer.Health);
}
}
public void TakeDamage()
{
thePlayer.Health -= 10;
}
}