Wrong animation is playing when I click on door

Hi, I am making a game which requires doors to be opened frequently. I have written the code below, however, when I first click the door, it polays the “DoorClose” animation when the “DoorOpen” animation should be playing. A solution would be very helpful.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorController : MonoBehaviour
{
    private Animation anim;
    private bool DoorIsClosed;
    void Start()
    {
        DoorIsClosed = true;
        anim = gameObject.GetComponent<Animation>();
    }
    void OnMouseDown()
    {
        if (DoorIsClosed == true)
        {
            anim.Play("DoorOpen");
            DoorIsClosed = false;
        }
        if (DoorIsClosed == false)
        {
            anim.Play("DoorClose");
            DoorIsClosed = true;
        }
    }
}

Nvm, I fixed it. The problem was that I should have had the second ‘if’ statement is and else{} bracket. Like this:

if (DoorIsClosed == true)
        {

            anim.Play("DoorOpen");
            DoorIsClosed = false;
        }
        else
        {
            if (DoorIsClosed == false)
                {

                    anim.Play("DoorClose");
                    DoorIsClosed = true;
                }
        }