Created
July 2, 2021 10:19
-
-
Save made-indrayana/159e29c704775d031da0c6d69f1c5b6c to your computer and use it in GitHub Desktop.
Making a recurring IEnum with Update-like behaviour for Unity
This file contains 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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class IEnumTest : MonoBehaviour | |
{ | |
public bool isTracking = false; | |
// Trigger to start, can be whatever | |
void Start() | |
{ | |
StartCoroutine(DebugTracking()); | |
} | |
// Trigger to stop | |
private void OnDestroy() | |
{ | |
StopAllCoroutines(); | |
} | |
IEnumerator DebugTracking() | |
{ | |
if (!isTracking) | |
{ | |
Debug.Log("I'm false"); | |
yield return new WaitForEndOfFrame(); // IMPORTANT otherwise it will start a loop of death | |
} | |
if (isTracking) | |
{ | |
Debug.Log("I'm true"); | |
yield return new WaitForEndOfFrame(); // IMPORTANT otherwise it will start a loop of death | |
} | |
// Circular reference to keep updating | |
StartCoroutine(DebugTracking()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment