in this script, it heals me when i am less than 100% health but when i am at 100% health, it gives me error message saying that coroutine is null, help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealingCircle : MonoBehaviour
{
public PlayerStats pS;
public int healthPerSecond;
Coroutine currentHealRoutine = null;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player") && pS.currentHealth < pS.maxHealth)
{
currentHealRoutine = StartCoroutine(Heal());
}
}
void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
StopCoroutine(currentHealRoutine);
}
}
IEnumerator Heal()
{
for (int _currentHealth = pS.currentHealth; _currentHealth < pS.maxHealth; _currentHealth += healthPerSecond)
{
if ((pS.currentHealth = _currentHealth) > pS.maxHealth) //Check if the current health is more than max health
{
pS.currentHealth = pS.maxHealth; //If yes, keep current health as max health
Debug.Log(pS.currentHealth);
}
else
{
pS.currentHealth = _currentHealth;
Debug.Log(pS.currentHealth);
yield return new WaitForSeconds(1f); //Wait for 1 second
}
}
pS.currentHealth = pS.maxHealth; //Set the current health to max health.
}
}