-
-
Save pencilking2002/eba2da5ab1b3e57d69fb4ab7ea86e79b to your computer and use it in GitHub Desktop.
For loop with delay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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