0

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 ?

WDUK
  • 1,412
  • 1
  • 12
  • 29

1 Answers1

2

Seems like something similar has been asked before: "Delegate subtraction has unpredictable result" in ReSharper/C#?

Based on the answer from the above post, it sounds like the warning isn't really relevant in your case.

FriedLychees
  • 114
  • 1
  • 9
  • Ah i see its coming from resharper. Thanks ! :) – WDUK Oct 13 '20 at 22:10
  • 1
    You now have enough reputation points to vote to close a question as duplicate - which is the right thing to do when you find a case like this. Thank you! – CoolBots Oct 13 '20 at 22:17