Time - interrupting an action

Hi. I started playing with Unity and C# few weeks ago, and I am going deeper and deeper everyday… ;). Today I want to write an algorithm, but I have encountered a problem with time that I can’t solve.

For sake of simplicity I have written points of what I want to achieve:

  1. There is an ACTION that requires for example 3 seconds to complete.

  2. If conditions are met (object is in ray, and space is pressed) - do ACTION (which takes 3 seconds)

  3. whenever ACTION is interrupted(at least one condition is not present) - stop ACTION

  4. whenever conditions are met again I want ACTION happen again and I want it to take again 3 seconds(do that action all over again)

I have almost done it, but the problem is:

I want to do something like timer:
every time conditions of action are met - "Set timer to 3 seconds and start that timer.

Update() - in this function check if action has completed successfully and also whenever conditions are not met “turn timer off”.

Thanks in advance.

UPDATE

I didn’t want to make it complex, but I think you are right, I should post my code. But I have changed few things since I posted it, maybe I mixed something. So, the idea is: Player controls a ship and he can ‘abduct’ objects. That’s the ‘Action’ I mentioned before.
using UnityEngine;
using System.Collections;

public class Abduction : MonoBehaviour
{
	GameObject player;
	Skills skills;
	GameObject ship;
	Transform shipTransform;
	Abductable abductable;
	bool isAbducting = false;
	float timeToFinish;
	
	void Start()
	{
		player = GameObject.Find ("Player");
		skills = player.GetComponent<Skills>();
		ship = GameObject.Find ("Ship");
		shipTransform = ship.transform;

	}
	
	void Update()
	{
		if(Input.GetKey (KeyCode.Space))
		{
			Ray ray = new Ray(shipTransform.position, shipTransform.up*-1);
			RaycastHit hit;
			Debug.DrawRay (ray.origin, ((shipTransform.up*-1)*skills.AbductionRange), Color.green);
			if (Physics.Raycast (ray, out hit, skills.AbductionRange))
			{
				if (hit.collider.CompareTag("Abductable"))
				{
					abductable = hit.collider.GetComponent<Abductable>();
					
					SetTimeToFinish (abductable.Weight + skills.AbductionSpeed);
					startAbduction(hit.collider, timeToFinish);
					
				}
				
			}
			
		}
		
	}
	void startAbduction(Collider objectToAbduct, float timeToFinishAbduction)
	{
		isAbducting = true;
		Debug.Log ("Abducting... Time to finish: " + timeToFinishAbduction + " Total time: " + Time.time);
		Ray ray = new Ray (shipTransform.position, shipTransform.up*-1);
		RaycastHit hit;
		if (Physics.Raycast (ray, out hit, skills.AbductionRange))
		{
			if (Time.time > timeToFinishAbduction)
			{
				Debug.Log ("Abduction FINISHED");
				Destroy (objectToAbduct.gameObject);	//destroy object
				isAbducting = false;
			}// else isAbducting = false;
			
		}
	}
	
	void SetTimeToFinish(float finishTime)
	{
		if (!isAbducting)
		{
			
			timeToFinish = finishTime + Time.time;
		} //else timeToFinish = 0f;
	}
	
	
	
}

Hey,
I’ve found a solution. It works perfectly. I’ve just learned Static things in C# and I’ve created my own timer, that I can reset, stop or start any time I want. So if anyone is having similar problem to mine, here is working source code.
So its designed for “2.5D” platformer, the effect is that player controls a ship and whenever spacebar is pressed he casts a ray that goes down (based on ship rotation). If anything is in ray that is ‘abductable’ ship will start abducting it. Abduction finishes in x seconds(based on player skill and weight of target). If target or ray didn’t change and all other conditions are met (its in range and spacebar is down) target becomes abducted (you decide what to do).

(just to note, that this is just experimental code, so there are points where I should use static variables/methods, or catch types, but I’m just playing around, so its not a serious code)

So, here is code:

using UnityEngine;
using System.Collections;

public class Abduction : MonoBehaviour
{
	GameObject player;
	Skills skills;
	GameObject ship;
	Transform shipTransform;
	Abductable abductable;
	bool isAbducting = false;
	float timeToFinish;
	
	Collider cacheTarget;  //cache target, so when ship is abducting script can refer to this object
	Vector3 cacheOriginTarget;
	
	void Start()
	{
		player = GameObject.Find ("Player");
		skills = player.GetComponent<Skills>();
		ship = GameObject.Find ("Ship");
		shipTransform = ship.transform;

	}
	
	void Update()
	{
		if(Input.GetKey (KeyCode.Space))
		{
			Ray ray = new Ray(shipTransform.position, shipTransform.up*-1);
			RaycastHit hit;
			Debug.DrawRay (ray.origin, ((shipTransform.up*-1)*skills.AbductionRange), Color.green);
			if (Physics.Raycast (ray, out hit, skills.AbductionRange))
			{
				if (hit.collider.CompareTag("Abductable"))
				{
					
					abductable = hit.collider.GetComponent<Abductable>();
					if (!isAbducting)
					{
						timeToFinish = skills.AbductionSpeed + abductable.Weight;
						Debug.Log ("TimeToFinish: " + timeToFinish);
						AbductionTimer.ResetTimer ();
						AbductionTimer.StartTimer ();
						isAbducting = true;
						
						//set start position
						cacheOriginTarget = hit.collider.transform.position;
						
						cacheTarget = hit.collider;
					}
					if (isAbducting)
					{
						if (hit.collider == cacheTarget)	// check if target hasn't changed
						{
							
							//HERE, 1.DECIDE WHAT TO DO WITH TARGET BEING ABDUCTED
							//      2.DECIDE WHAT EVENTS SHOULD HAPPEN DURING 'ABDUCTION'
							
							
							if(AbductionTimer.CurrentTime >= timeToFinish)
							{
								//HERE DECIDE WHAT TO DO WHEN ABDUCTION IS FINISHED
								Debug.Log ("ABDUCTION FINISHED");
								Destroy (hit.collider.gameObject);
								isAbducting = false;
							}
						} else isAbducting = false;

						
					}
				}else isAbducting = false;

			} 
				
		} else isAbducting = false;
		
		if (!isAbducting)
		{
			AbductionTimer.StopTimer ();
			AbductionTimer.ResetTimer ();
			cacheTarget = null;
		}
			
	}
	
	void OnGUI()
	{
		GUI.Box (new Rect(10,65,150,25), "isAbducting: " + isAbducting);
		GUI.Box (new Rect(10,100,600,25), "cached target: " + cacheTarget);
		
	}
	
	
}