- Home /
Player lose health ontriggerenter2d with coroutine
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;
}
}
}
Answer by Batuhan13 · Mar 02, 2020 at 04:57 AM
Actually your code is looking good .Could you try add debug line in On trigger enter function "line 7" if it works there is a problem about your tag proably your player have a wrong tag else if proably problem is rigidbody 2d .Could you check are player and laser gameobject have a rigidbody 2d component if they have then problem is about components .Could you check are both gameobject have a rigidbody 2d and collision 2d .I hope these will solve your problem =)
I have put rigidbody2d for both player and laser. Still didn't work.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Creating an app for mobile and browsers 1 Answer
Adapt OnMouseDown to control with Gamepad? 0 Answers
tags on ContactFilter2D? 0 Answers