I would like to make an object add X amount of life to my character, but I don’t understand how to do it. I am a newbie on the subject of development and programming
public class VidaExtra : MonoBehaviour
{
public int CantidadDeVida;
Health Vida;
private void Awake()
{
Vida = GetComponent<Health>();
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Vida.currenthealth =++ CantidadDeVida;
}
}
}
public class Health : MonoBehaviour
{
public int startinghealth = 100;
public int currenthealth;
public Slider healthSlider;
public Image damageImage;
public float flashspeed = 5f;
public Color flashcolor = new Color(1f, 0f, 0f, 0.1f);
public GameObject PersonajeVivo;
public GameObject PersonajeMuerto;
public Animator AnimaiconMuerte;
public GameObject explosion;
public GameObject PantallaEnNegro;
PlayerController player;
PlayerShooting PlayerShooting;
bool isDead;
bool damage;
public float TiempoEntreVida = 0.15f;
/* public float TiempoStart = 0;
public float TiempoEnd = 0;
public string Escena;
*/
private void Awake()
{
currenthealth = startinghealth;
PlayerShooting = GetComponent<PlayerShooting>();
player = GetComponent<PlayerController>();
StartCoroutine(ReloadAmmo());
}
IEnumerator ReloadAmmo()
{
float reloadTime = TiempoEntreVida * 7;
while (true)
{
yield return new WaitForSeconds(reloadTime);
if (currenthealth < startinghealth)
{
currenthealth++;
healthSlider.value = currenthealth;
}
}
}
void Start()
{
if (damage)
{
damageImage.color = flashcolor;
}
else
{
damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashspeed * Time.deltaTime);
}
damage = false;
PersonajeVivo = GameObject.Find("Personaje 1");
PersonajeMuerto = GameObject.Find("PersonajeAnimacionMuerte");
PersonajeMuerto.SetActive(false);
AnimaiconMuerte.enabled = false;
PantallaEnNegro = GameObject.Find("Image");
PantallaEnNegro.SetActive(false);
}
void Update()
{
}
public void TakeDamage(int amount)
{
damage = true;
currenthealth -= amount;
healthSlider.value = currenthealth;
if (currenthealth <= 0)//&&!isDead)
{
PersonajeVivo.SetActive(false);
PersonajeMuerto.SetActive(true);
player.enabled = false;
AnimaiconMuerte.enabled = true;
Instantiate(explosion, transform.position, transform.rotation);
PantallaEnNegro.SetActive(true);
// Destroy(gameObject, 10f);
// Destroy(gameObject);
/* TiempoStart += Time.deltaTime;
if (TiempoStart >= TiempoEnd)
{
SceneManager.LoadSceneAsync(Escena);
}
*/
}
// void Death()
//{
//isDead = true;
//PlayerShooting.DisableEffects();
// PlayerShooting.enabled = false;
// }
}
}