- Home /
How to continue the work in onTriggerStay, when it disables, for remaining objects?
Hello everyone, my problem here is,
i want objects that collides with my spherical collider to do some work, while they are inside of it.
However, i'll completely disable the collider. Then, While some objects are doing the work, may stop working when it is disables. I want objects that once collides with collider, to finish the work until the end, even if the collider is being disabled.
I don't want to start too much coroutines for much objects at the same time.
void OnTriggerStay(Collider other)
{
if (other.CompareTag("Tag"))
{
//Work..
}
}
First solution, has came to mind causes infinite while loop of course:
void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Tag")
{
while(true)
{
//Work..
}
}
}
Is there any solution idea, can solve the problem? Thank you in advance..
Answer by misher · Oct 11, 2019 at 09:50 AM
Why would you make a while loop. If you want some work to be done over several frames you can use coroutines. You can also create and attach a specific component (MonoBehaviour script) to make a work on your objects within coroutines or inside an update loop, then destroy it when needed (upon reaching some condition).
Answer by xxmariofer · Oct 11, 2019 at 10:20 AM
Ontrigger events can be used like coroutines
IEnumerator OnTriggerEnter(Collider other)
{
if(other.CompareTag("Tag")
{
while(true)
{
yield return null;
}
}
}
Can you clarify what that would mean? Start a coroutine on OnTriggerEnter that stops on OnTriggerExit? That seems pretty convenient, but surprising.
Yes basically use a variable like bool colliding set yo true when the coroytine gets called, and set it back to false on the ontriggerexit and ins$$anonymous$$d of a while(true) use while(colliding)
Hace you test using just the ontriigerstay event?
Your answer
Follow this Question
Related Questions
Simple Colliders Question 1 Answer
Using a trigger to align an object? 1 Answer
OnTriggerEnter firing twice 1 Answer
Trigger2D not working 0 Answers