Skip to content

Instantly share code, notes, and snippets.

@pencilking2002
Last active December 12, 2020 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pencilking2002/eba2da5ab1b3e57d69fb4ab7ea86e79b to your computer and use it in GitHub Desktop.
Save pencilking2002/eba2da5ab1b3e57d69fb4ab7ea86e79b to your computer and use it in GitHub Desktop.
For loop with delay
public int[] myArr = new int[3];
public float delay = 5.0f;
private float lastDebugTime;
private int currIndex;
// For keeping track of the index in a foreach loop
private float foreachIndex;
private void Update()
{
// For loop method
for (int i=0; i<myArr.Length; i++)
{
if (Time.time > lastDebugTime + delay && currIndex == i)
{
currIndex++;
if (i == myArr.Length-1)
currIndex = 0;
Debug.Log(i);
lastDebugTime = Time.time;
}
}
// foreach method
foreach(Vector3int position in activeTM.cellBounds.allPositionsWithin)
{
if (Time.time > lastDebugTime + delay && currIndex == foreachIndex)
{
currIndex++;
if (foreachIndex == myArr.Length-1)
currIndex = 0;
activeTM.SetTile(position, seasonMaps[to].GetTile));
lastDebugTime = Time.time;
}
foreachIndex++;
// When using a foreach loop, you need to keep track of the index manually
if (foreachIndex == activeTM.cellBounds.allPositionsWithin.Length-1)
foreachIndex = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment