Search Unity

Co-routines causing lag. Looking for alternatives or work around.

Discussion in 'Editor & General Support' started by Towsif-Khan, Apr 16, 2019.

  1. Towsif-Khan

    Towsif-Khan

    Joined:
    Jan 9, 2016
    Posts:
    4
    I have a few coroutines that run infinitely by calling themselves and are is initially called from inside the Start() function. According the profiler these coroutines are solely responsible for causing massive drops in fps after only a few minutes of play.

    I have been looking for solutions on various forums but all of the ones I have found were about using coroutines in the Update() function. Which is different from what I am doing and hence I am not aware of any alternatives.
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,455
    If your goal is to run them indefinitely, you do not need them to recursively call themselves. They can just contain a
    Code (CSharp):
    1. while(true){
    2. // do stuff
    3. yield return null;
    4. }
    Recursion means every new cycle is a new managed allocation for creating the co-routine.

    What specifically does the profiler tell you is causing the lag and we'd need a bit more details on the setup of these cor-outines, e.g. what co-routines start which other ones, how does the entire thing scale up and what are you doing within them?
     
  3. Towsif-Khan

    Towsif-Khan

    Joined:
    Jan 9, 2016
    Posts:
    4
    Thanks. I shall try the infinite loop first. If nothing changes I shall try to find and post everything you have mentioned above.