I have constructed an infinite loop that is constantly calculating camera distance from specified objects.
To free up my Update method of clutter and to stop and resume the loop on a whim without completely disabling my game object, I am doing this in a coroutine.
Because of this, my entire Unity runtime seems to stall when the coroutine is started. Is this really being treated as a blocking call even after the first and only yield
command?
void Start () {
if (_cam == null) {
_cam = MainCamera.instance.GetGameObject();
_calculateCameraRange = StartCoroutine("CalculateCameraRange");
Debug.Log("Object started");
}
}
void OnEnable () {
_calculateCameraRange = StartCoroutine("CalculateCameraRange");
}
void OnDisable () {
StopCoroutine(_calculateCameraRange);
}
private Coroutine _calculateCameraRange;
IEnumerator CalculateCameraRange () {
yield return new WaitForSeconds(0.1f);
for (;;) {
if (Vector3.Distance(_cam.transform.position, gameObject.transform.position) > _distanceThreashold) {
/// Actions to take when camera is out of range.
_isInRange = false;
if (_isCoolingDown) continue;
_holoCursorBlue.SetActive(true);
foreach (var o in _holoPanelList) {
o.SetActive(false);
}
StartCoroutine("Cooldown");
} else {
_isInRange = true;
if (_isCoolingDown) continue;
/// Actions to take when camera is in range.
_holoCursorBlue.SetActive(false);
_holoCursorAmber.SetActive(false);
foreach (var o in _holoPanelList) {
o.SetActive(true);
}
if (_flashInjuryAlert != null) StopCoroutine(_flashInjuryAlert);
StartCoroutine("Cooldown");
}
}
yield return null;
}