Refactor not working as expected

Following the logic for guarding against the NaN error worked as expected until I tried to refactor it, then the error returns.

Probably missing something really obvious, refactoring not essential, just thought I would try and start tidying up Update.

The function won’t return out of Update, only itself. The code in Update will continue to execute after the function. In this case, the return in the function is unnecessary because it’s the last thing that will happen anyway.

You’d want to return a value that you can check in Update. Something like this

private bool PeriodIsZero() // <- return a boolean
    if (Mathf.Approximately(period, 0f)) // <- this checks if period is _almost_ 0, but _almost_ 0 will actually be fine
        Debug.LogWarning("period cannot = 0!"); // <- log a warning instead of just a message (could be error, too)
        return true; // <- return true, because the function checks if period is zero, and here it is
    return false; // <- period is not zero

and then you can use this in Update

private void Update()
    if (PeriodIsZero()) { return; } // <- call the function, and exit if it returns 'true'

Note: I changed the function name, but it can be whatever you feel is needed

1 Like

Thanks, that did the trick :+1:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.