How do I change the texture of a gameobject only while a touch is on it on iOS?

I have buttons that aren’t using the GUI system, but instead are just textures on gameobjects. I’m using raycast.hit to detect whether the button is hit. I want the button to swap textures while a touch is on it to give the visual effect of being pressed down. I haven’t been able to get the picture to switch back to it’s original when the touchphase ends.

function Update(){
if (fingerCount>0||Input.GetMouseButtonDown(0)) {	
var raybutton: Ray;
if ((fingerCount>0)&&(Input.GetTouch(0).fingerId==0)&&(Input.GetTouch(0).phase==TouchPhase.Began)){
		raybutton= OrthoCam.ScreenPointToRay(Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y));
}
var hitbutton: RaycastHit;
if (Physics.Raycast(raybutton, hitbutton)) {
			var buttonName=hitbutton.transform.gameObject.name;

if (buttonName=="Restart"){
		GameObject.Find("Restart").renderer.material.mainTexture=Resources.Load("RestartPressed");
var restartflag=true;
}
}


for (var touch = 0; touch < Input.touchCount; ++touch) {
   if (Input.GetTouch(touch).phase == TouchPhase.Ended||Input.GetTouch(touch).phase == TouchPhase.Canceled) {
		if(restartflag==true){
GameObject.Find("Restart").renderer.material.mainTexture=Resources.Load("Restart");
		restartflag=false;

		}
	}
}

}

If you initialize restartFlag inside an if conditional, it won’t exist outside of that if conditional, so when you reach if (restartFlag) that variable does not exist anymore. You need to initialize it outside of Update() if you want its state to persist between calls to Update().