Adding yield to spread onTrigger script workload over frames to prevent lag

I have a script with OnTrigger setting off some functions. From the profiler I see that all of my triggers may be the causing of some lag spikes.

One idea was to spread the work load of these coroutines over more frames instead of it all happening on the trigger frame.

Would I do this by adding yield many times into my script?

eg. do something, yield, do something, yield, do something, yield?

In practice this seems to work although im wondering is there a reason I should not do this? Is there a more elegant alternative (threads seem overkill?)

Many thanks in advance!

J

What are you doing in that OnTrigger frame? It could be the detection, do you have multiple OnTrigger collisions? Although I don’t think yield is the best option, it depends.

Thanks for the quick reply!

What would you use instead of yield?

Thanks,

J