Calling an loop over a frame?

Hey guys I have a for loop inside of a Invoke that is getting huge spikes when I loop through it dependent on how long the loop is.

I know there is a way to do this loop over a few frames with 60 calls a second, I just forgot how. Could you please remind me? Also what is the name of this pattern?
Thank you!

You need a coroutine. Very easy to do.

I’ve just rushed copied/pasted your code - not compilable, but you get the idea.
You want to move your loop into a Coroutine, and WAIT within your iterations.
e.g. You could do

yield return new WaitForFixedUpdate()

or just specify a delay in each loop. This will smear your processing over time - and not spike it in one synchronous lump.

e.g.

 using UnityEngine;
 using System.Collections;
 
 public class NPCManager : MonoBehaviour 
 {
     int currentNumberOn = 0;

    private Coroutine _looper;

     void Names()
     {
         if (GameManager.Instance.lCharacters.Count > 0)
         {
             _looper = StartCoroutine(Loop());
            
             if (currentNumberOn < GameManager.Instance.lCharacters.Count)
             {
                 Debug.Log(GameManager.Instance.lCharacters[currentNumberOn]._Name + " Energy is at " + GameManager.Instance.lCharacters[currentNumberOn]._Energy);
                 currentNumberOn++;
             }
             else
             {
                 currentNumberOn = 0;
             }
         }
     }

    IEnumerator Loop() {
         for (int i = 0; i < GameManager.Instance.lCharacters.Count; i++)
             {
                 Debug.Log(GameManager.Instance.lCharacters<em>._Name + " Energy is at " + GameManager.Instance.lCharacters*._Energy);*</em>

yield return new WaitForSeconds(0.05f);
}
}

void Start()
{
InvokeRepeating(“Names”, 0, 0.1f);
}
}