Hello good day, that’s my question, I leave my code assigned to my melee enemy. Thanks!
using UnityEngine;
public class Enemymelee : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("PLayer"))
{
GameManager.Instance.LoseHealth(5);
}
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public Text ammotext;
public static GameManager Instance { get; private set; } // SUPUESTAMENTE SE LLAMA SINGLENTON
public int gunAmmo = 10;
public int health = 100;
public Text healthtext;
public int maxhealth=100;
private void Awake()
{ Instance = this; }
private void Update()
{ ammotext.text = gunAmmo.ToString();
healthtext.text = health.ToString();
}
public void LoseHealth(int healthtoreduce)
{
health -= healthtoreduce;
CheckHealth();
}
public void CheckHealth()
{
if (health < 0)
{
Debug.Log("Has muerto");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
public void AddHealth(int health)
{
if (this.health +health >= maxhealth )
{ this.health = 100; }
else
{ this .health += health; }
}
}