Skip to content

Instantly share code, notes, and snippets.

@made-indrayana
Created July 2, 2021 10:19
Show Gist options
  • Save made-indrayana/159e29c704775d031da0c6d69f1c5b6c to your computer and use it in GitHub Desktop.
Save made-indrayana/159e29c704775d031da0c6d69f1c5b6c to your computer and use it in GitHub Desktop.
Making a recurring IEnum with Update-like behaviour for Unity
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