How to Detect Which Sprite a Sprite Renderer Is Using?

Hello, everyone. I have a stupid question: I want to set a boolean to true if an object’s sprite renderer is using a certain sprite, however I am unsure how to do so. When I use this code I get an error which says “The name sprite does not exist in the current context”:

bool myBool;
SpriteRenderer sr;

void Start () {
        sr = GetComponent<SpriteRenderer>();
    }

void Update () {
        if (sr.sprite == mySprite)
            {
                myBool = true;
            }
}

If anyone knows how to do this correctly, please tell me. Thanks.

Your code is fine, so the problem is mySprite never be assigned (not sure if its in inspector or where) or the spriterenderer of the object who has that monobehaviour doesnt have a sprite. i have just tested and works fine.