# execute for x seconds \<how?\>

**URL:** <https://discussions.unity.com/t/execute-for-x-seconds-how/465445>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [January 12, 2012, 7:32am UTC](https://discussions.unity.com/t/execute-for-x-seconds-how/465445 "2012-01-12T07:32:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![belias](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/belias/32/220597_2.png) [@belias](https://discussions.unity.com/u/belias)\
**Post date:** [January 12, 2012, 7:32am UTC](https://discussions.unity.com/t/execute-for-x-seconds-how/465445/1 "2012-01-12T07:32:05Z")

</div>

hello

I want to know how many ways are there to execute a command for 5 seconds for example.

second 0 : a = 1  
second 1 : a = 2  
second 2 : a = 3  
second 3 : a = 4  
second 4 : a = 5  
second 5 : a = 6  
second 6 : a = 6  
second 7 : a = 6  
.  
.  
.

how many ways are there, easy and hard ones.

---

<div class="post-metadata">

**Author:** ![justinlloyd](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@justinlloyd](https://discussions.unity.com/u/justinlloyd)\
**Post date:** [January 12, 2012, 7:48am UTC](https://discussions.unity.com/t/execute-for-x-seconds-how/465445/2 "2012-01-12T07:48:14Z")

</div>

The answer is “lots” because the question is too broad and mostly meaningless.

---

<div class="post-metadata">

**Author:** ![belias](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/belias/32/220597_2.png) [@belias](https://discussions.unity.com/u/belias)\
**Post date:** [January 12, 2012, 8:56am UTC](https://discussions.unity.com/t/execute-for-x-seconds-how/465445/3 "2012-01-12T08:56:38Z")

</div>

thanks, ok, I want a command to get executed [ON] for x seconds then it is [OFF] , clear now?

---

<div class="post-metadata">

**Author:** ![abbc](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@abbc](https://discussions.unity.com/u/abbc)\
**Post date:** [January 12, 2012, 9:12am UTC](https://discussions.unity.com/t/execute-for-x-seconds-how/465445/4 "2012-01-12T09:12:29Z")

</div>

//delay  
delay = 10;

//store current time in a variable  
startTime = Time.time;

…

[//Time.time](https://Time.time) is current time  
if (Time.time \>= (startTime + delay))

…

---

<div class="post-metadata">

**Author:** ![BooBi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/boobi/32/215944_2.png) [@BooBi](https://discussions.unity.com/u/BooBi)\
**Post date:** [January 12, 2012, 10:56am UTC](https://discussions.unity.com/t/execute-for-x-seconds-how/465445/5 "2012-01-12T10:56:43Z")

</div>

```auto
   void Update()
    {
        if(actionON == true)
            Do Action;
    }

    IENumerator TimedAction()
    {
        actionON = true;
        yield return new WaitForSecond(5);
        actionON = false;
    }

```

And you need something to launch the coroutine like a button:

```auto
void OnGUI()
    {
        if (GUI.Button(Rect, ""))
        {
            StartCoroutine("TimedAction");
        }
    }

```

---

<div class="post-metadata">

**Author:** ![belias](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/belias/32/220597_2.png) [@belias](https://discussions.unity.com/u/belias)\
**Post date:** [January 24, 2012, 8:58pm UTC](https://discussions.unity.com/t/execute-for-x-seconds-how/465445/6 "2012-01-24T20:58:00Z")

</div>

line 47 brings an error

```auto
using UnityEngine;
using System.Collections;

public class MoveObjectAsTouch : MonoBehaviour 
{
	
	public string MovableObjectTag;	//Disk
	
	private Transform _myTransform;
	
	private string _currentState;
	
	private Ray _ray;
	
	private RaycastHit _hit;
	
	private bool _rightObject;
	
	private Vector3 _initialPos;
	private Vector3 screenPos;
	private Vector3 offset;
	private Vector3 currentScreenPos;
	private Vector3 currentPos;
	private Vector3 F1;
	private Vector3 F2;
	private Vector3 M;

	
	void Start()
	{
		_myTransform = transform;
	}
	
	void OnGUI()
	{
		//GUI.Label(new Rect(10, 10, 100, 30), _currentState);
	}
	
	void FixedUpdate()
	{
		//////
		{
        if(actionON == true)
            print("launch");
    	}
		
		IENumerator TimedAction()
   		{
        	actionON = true;
        	yield return new WaitForSecond(5);
        	actionON = false;
  		}
		//////

		if (iPhoneInput.touchCount == 1)
		{

            Touch firstTouch = Input.GetTouch(0);
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            RaycastHit hit;

            if (Physics.Raycast (ray, out hit, 200)) 
			{
                if(hit.collider.tag == MovableObjectTag)
				{
                    if (firstTouch.phase == TouchPhase.Began) 
					{
						_currentState = "Touch's began";
						
                        screenPos = Camera.main.WorldToScreenPoint(_myTransform.position);
                        offset = _myTransform.position - Camera.main.ScreenToWorldPoint(new Vector3(firstTouch.position.x,firstTouch.position.y,screenPos.z));   
					}
					else if(firstTouch.phase == TouchPhase.Moved)
					{
						_currentState = "Touch's Moved";
						
                        currentScreenPos = new Vector3(firstTouch.position.x, firstTouch.position.y, screenPos.z); // firstTouch.position.y
                        currentPos = Camera.main.ScreenToWorldPoint(currentScreenPos) + offset;
                        _myTransform.position = currentPos;
                	}
					else if(firstTouch.phase == TouchPhase.Ended)
					{
						_currentState = "Touch's Ended";
						 StartCoroutine("TimedAction");
						//rigidbody.isKinematic = false;
						
					}
            	}   
        	}       
   	 	}
	}
}

```

---

<div class="post-metadata">

**Author:** ![KelsoMRK](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelsomrk/32/205687_2.png) [@KelsoMRK](https://discussions.unity.com/u/KelsoMRK)\
**Post date:** [January 24, 2012, 9:04pm UTC](https://discussions.unity.com/t/execute-for-x-seconds-how/465445/7 "2012-01-24T21:04:57Z")

</div>

1. its IEnumerator not IENumerator.
2. You have a method TimedAction -inside- another method FixedUpdate
