unity c# run a function every secind code example

Example 1: unity call function on update once per second

float elapsedTime;
float timeLimit = 0.1f;

private void Update()
{
    elapsedTime += Time.deltaTime;
    if (elapsedTime >= timeLimit)
    {
        elapsedTime = 0;
        //Rest of your code or function call goes here
    }
}

Example 2: have a command only run for a few seconds unity

StartCoroutine(GoLeft());
 IEnumerator GoLeft()
 {
     // This will wait 1 second like Invoke could do, remove this if you don't need it
     yield return new WaitForSeconds(1);
 
 
     float timePassed = 0;
     while (timePassed < 3)
     {
         // Code to go left here
         timePassed += Time.deltaTime;
 
         yield return null;
     }
 }