I have a small bit of code where i assign a method to an action when an animation is completed then i want to remove it under a certain condition. But it warns me that this might lead to an unpredictable result.
This is the code:
void Init()
{
_animation.OnAnimationComplete += OnComplete;
}
private void OnComplete()
{
_isLoaded = !_isLoaded;
if (_isLoaded)
{
// remove the callback when loaded since we don't need it afterwards
// this gives me the warning
_animation.OnAnimationComplete -= OnComplete;
}
}
public void LoadPanel() => _aimation.FadeIn();
public void UnloadPanel() => _animation.FadeOut();
The warning i get is
Delegate subtraction has unpredictable result
OnAnimationComplete
is an Action
type by the way.
Am i not writing good code here? What is the correct way to do this ?