Trying to make an open/close door on click and its simply not working!

Hello forum users! I am hoping to get some help with this issue that has me officially stumped after fits of rage and lots of googling. I have a door that I am able to have open on clicking, once open I want it to close. According to logic (and other code I’ve used as examples) my code should work… but for some reason that is beyond my knowledge it simply refuses to work as it should! I do not get it!

Here is my code:

using UnityEngine;
using System.Collections;

public class DoorController : MonoBehaviour {
	private  bool isMouseInside = false;
	private  bool isDoorOpen =  false;
	
	// Use this for initialization
	void Start () {
		
	}
	
	void OnMouseEnter() {
		isMouseInside = true;
	}
	
	void OnMouseExit() {
		isMouseInside = false;
	}
	
	// Update is called once per frame
	void Update() {
		if(isMouseInside && Input.GetButtonDown("Fire2")) {
			
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast(ray, out hit)) {
				BoxCollider bc = hit.collider as BoxCollider;
				Animator anim = GetComponent<Animator>();
				if (bc != null){
					if(isDoorOpen){
						Debug.Log("Closing the door");
						anim.Play("CloseDoor");
					}
					else {
						Debug.Log("Opening the door");
						anim.Play("OpenDoor");
					}
					isDoorOpen = !isDoorOpen;
				}
			}
	    }
	}
}

It seems to open the door open clicking, but then it stops doing anything after that, it seems to ignore the if statement and simply refuse to play the close animation or send anything to the console. Any help is much appreciated!

Hi,
using OnMouseUpAsButton will save you some work I believe.

You could then do :

    void Awake()
    {
            Animator anim = GetComponent<Animator>();
    }
    void OnMouseUpAsButton()
    {
                     if(isDoorOpen){
                         Debug.Log("Closing the door");
                         anim.Play("CloseDoor");
                     }
                     else {
                         Debug.Log("Opening the door");
                         anim.Play("OpenDoor");
                     }
                     isDoorOpen = !isDoorOpen;
             }
         }
     }

Now, I would probably expose a bool parameter from the Animator Controller to set the transition conditions between Open and Close, and simply manipulate this parameter from the code.