HELP! How to make Update function start after delay? C#
Hey guys,
I'm new to unity and programming and I'm having some issues I hope you guys can help me with. I'm trying to create a delay before starting the Update function (is it even possible?). The script is for my character movement and I want there to be a slight delay before the user can start to move the character. This is what I have so far:
Sorry I had problems putting the Code in here so i took a picture.
So basically I want to make a short delay - maybe 2-3 seconds in which the player cant control the character. If there is any way I can accomplish this please help me out!
Thanks in advance!
Answer by Positive7 · Sep 04, 2015 at 12:18 PM
float time = 3.0f;
void Update () {
if(time >= 0){
time -= Time.deltaTime;
return;
}else{
//Do Something after clock hits 0
}
//Do Something else while clock counting down
}
Thanks a lot! It worked but if you dont $$anonymous$$d could you explain why we write return; ?
Sorry you can delete return;
I was going to do time -= Time.deltaTime;
outside the if
statement. Sent from mobile so I left it in by accident.
Answer by Inok · Sep 29, 2015 at 11:41 PM
For delay in code execution better use coroutines Coroutines
Learn them and you will not want to back to primitive timers in update.
I just wanted to post an alternative to the already accepted answer that does not require a check within "Update".
float delay = 1.0f;
private void Awake()
{
Invoke(nameof(LateStart), delay);
}
void LateStart()
{
// your code
}
// to lean in on the comment of "Inok" suggesting a coroutine, this is how that code could look like:
float delay = 1.0f;
private void Awake()
{
StartCoroutine(_LateStartRoutine(delay));
}
IEnumerator _LateStartRoutine(float delay)
{
yield return new WaitForSeconds(delay);
// your late start code
}
Your answer
Follow this Question
Related Questions
Keeping Score out of Update Function 1 Answer
Rotate Player 90 degrees about its Y axis relative to the mouse being dragged between two angles 1 Answer
How to make a thrown object land on a certain point e.g a thrown spear landing on its tip 0 Answers
Having trouble deleting objects from a list after they reach a certain scale. 0 Answers
Problems with respawning using a very simple script 1 Answer