Search Unity

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

Discussion in 'Scripting' started by shmomo, Nov 28, 2012.

  1. shmomo

    shmomo

    Joined:
    Oct 11, 2011
    Posts:
    127
    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
     
  2. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    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.
     
  3. shmomo

    shmomo

    Joined:
    Oct 11, 2011
    Posts:
    127
    Thanks for the quick reply!

    What would you use instead of yield?

    Thanks,

    J
     
    Last edited: Nov 29, 2012