using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class PlayerHealth : MonoBehaviour { public Slider healthBar; public Text healthText; public int pHealth = 10; // Start is called before the first frame update void Start() { if(DataManager.playerHealth > 0) { pHealth = DataManager.playerHealth; } healthText.text = "Health: " + pHealth; healthBar.maxValue = pHealth; healthBar.value = pHealth; } // Update is called once per frame void Update() { } void OnCollisionEnter2D(Collision2D collision) { GameObject otherObject = collision.gameObject; if(otherObject.tag == "EnemyTag" ) { pHealth--; healthText.text = "Health: " + pHealth; healthBar.value = pHealth; if(pHealth <=0) { SceneManager.LoadScene("Death"); //Destroy(gameObject); } } } void OnTriggerEnter2D(Collider2D collision) { GameObject otherObject = collision.gameObject; if (otherObject.tag == "EnemyBullet") { pHealth--; healthText.text = "Health: " + pHealth; healthBar.value = pHealth; Debug.Log("Player Health: " + pHealth); if (pHealth <= 0) { SceneManager.LoadScene("Death"); //Destroy(gameObject); } } } }