Question by
SteenPetersen · Jun 09, 2017 at 04:43 PM ·
ontriggerentercollision2dontriggerstay
OnTriggerEnter2D not reseting when collider gets disabled
Hello there,
I am trying to use a collider2D on a child object to detect when something is hit. I order to do this I enable the collider as so:
public void Hit()
{
hitBox.enabled = true;
StartCoroutine("HitBoxLifeTime");
}
I then disable the hit box shortly after with a coroutine:
IEnumerator HitBoxLifeTime()
{
yield return new WaitForSeconds(.1f);
hitBox.enabled = false;
}
In that breif interval I would like for the collider to inform me if it has collided with a tag of my choosing. But it seems to only perfrom this check once.
An attempt I made to fixthis was to use OnTriggerStay2D and simply use a bool to make sure the check only happens once but this does not seem to work either as for some reason it doesnt seem to keep sending collision data indefinately, so I assume I am doing something wrong.
void Start () {
parent = GetComponentInParent<Navigator>();
}
public void Hit()
{
hitBox.enabled = true;
StartCoroutine("HitBoxLifeTime");
hitAllowed = true;
}
void OnTriggerStay2D(Collider2D other)
{
if (hitAllowed)
{
Debug.Log(string.Format("collided with {0}", other));
parent.HitBoxCollisionDetection(other);
}
}
IEnumerator HitBoxLifeTime()
{
yield return new WaitForSeconds(.1f);
hitAllowed = false;
hitBox.enabled = false;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612123839im_/https://answers.unity.com/themes/thub/images/avi.jpg)