- Home /
How do you add a time delay to this script ?
I am trying to move a end game trigger column to the left after a certain amount of time. How would I go about adding a time delay so that it can be spawned after 10 seconds.
This is my code.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class move: MonoBehaviour {
public float speed;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
transform.position += Vector3.left * speed * Time.deltaTimes ;
}
}
Answer by promant12 · Jul 08, 2019 at 03:53 PM
Make another script containing the following code:
public move moveScript
void Start()
{
StartCoroutine(WaitForMove())
}
IEnumerator WaitForMove()
{
yield return new WaitForSeconds(10f);
moveScript.enabled = true;
}
Disable your 'move' script in the inspector by hitting small box next to the script's name and voilà, it works just perfect.
Answer by JonPQ · Jul 08, 2019 at 03:39 PM
a few ways to do this.... make another function... say DoMove() then instead of calling it directly, call it using Invoke(functionName, delay); e.g. Invoke("DoMove", delay);
or use a CoRoutine.... a co routine executes a bit at a time, so you can add delays into the function. https://docs.unity3d.com/Manual/Coroutines.html
Your answer
Follow this Question
Related Questions
Multiply deltaTime to arrive at point2 in 1.5 sec ? 1 Answer
Having trouble increasing the spawning speed of explosions 0 Answers
Is there a way to speed up the game more than 100 times? 1 Answer
Time does not start counting down when need to 1 Answer
CountDown Timer Help (Seconds problem) 2 Answers