Why is my door opening and closing at the same time?????????

Im not able to open the door and then close it by touch. My script opens the door and closes it immediately. It does not regonize if the boolean is true or not. it plays the animation at the same time. I want to touch the door then it should open. By the second touch it should close.

I tried a lot but no chance to fix it. The script is attachet to a door!!!

heres my script.//////////////////////////////////////////////////

private var focusObj:GameObject=null; // null Bedeutet das es leer ist

var dorop:boolean= false;

function Update ()


if(Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Began) // checkt ab ob der finger ber¸hrt


	focusObj = null;		// null Bedeutet das es leer ist

	var ray =Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

	var hit : RaycastHit; 

				if(Physics.Raycast(ray,hit,100000) && hit.transform.tag == "door" && dorop == false)
				print ("dorop=true");
				focusObj = hit.transform.gameObject;		// der Var focusObj wird das GameObjekt zugewiesen.
				if(Physics.Raycast(ray,hit,100000) && hit.transform.tag == "door" && dorop == true)
				animation.Play("Door CloseVorrat");
				print ("dorop=false");

if (focusObj&&Input.touchCount<0&&Input.GetTouch(0).phase == TouchPhase.Ended) // checkt ab ob der finger noch da ist



So why it does not work??? Please can help me someone???

I think it’s because on the first frame you touch the door and start the animation and set the dorop to true, then immediately comes the second frame and you probably still have the finger on the door (unless you’re really, really fast) and this time the dorop variable is true so the door immediately closes. You should set dorop to true only when the animtion is complete.

Thank you guys I got it with coroutines.