I want to make player lose health when player hit a certain object. The object appear and disappear with coroutine. It didn’t work somehow. Any solution?
public class Coroutine : MonoBehaviour
{
public GameObject laser;
public int laserOnOff;
void Start()
{
StartCoroutine(LaserFlickering());
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
HealthControl.health -= 1;
}
}
IEnumerator LaserFlickering()
{
while (laserOnOff < 100)
{
yield return new WaitForSeconds(2);
laser.SetActive(false);
yield return new WaitForSeconds(2);
laser.SetActive(true);
laserOnOff += 1;
}
}
}