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;
}
}
Example 2: have a command only run for a few seconds unity
StartCoroutine(GoLeft());
IEnumerator GoLeft()
{
yield return new WaitForSeconds(1);
float timePassed = 0;
while (timePassed < 3)
{
timePassed += Time.deltaTime;
yield return null;
}
}