• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
18
Question by Jan_Julius · Sep 24, 2014 at 01:39 PM · timedelay

[c#] How can I let something happen after a small delay?

How can I add a small delay between things happening in a function, after the delay it should continue with what it was doing? in C#

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jun 23, 2020 at 08:05 PM 0
Share

it's just Invoke. that's all there is to it. there's also InvokeRepeating if you want it to happen over and over. it's absurd to dabble with coroutines for something so simple.

7 Replies

· Add your reply
  • Sort: 
avatar image
47
Best Answer

Answer by dmg0600 · Sep 24, 2014 at 01:44 PM

You can do it with coroutines in any MonoBehaviour:

 IEnumerator ExecuteAfterTime(float time)
 {
     yield return new WaitForSeconds(time);
 
     // Code to execute after the delay
 }

You can call it with, for example, StartCoroutine(ExecuteAfterTime(10));

Note that it will execute asynchronous so put everything that has to wait for it to be done, inside the coroutine.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jan_Julius · Sep 24, 2014 at 01:46 PM 0
Share

But, when I call it from the update fuction befor the function before doing this will it not be called multiple times?

avatar image Andres-Fernandez · Sep 24, 2014 at 01:48 PM 0
Share

Call it somewhere else (like Start function). Check the tutorial.

avatar image Jan_Julius · Sep 24, 2014 at 01:50 PM 0
Share

But what if it's done through there? how can I make it so it will only be executed once?

avatar image Andres-Fernandez · Sep 24, 2014 at 01:52 PM 0
Share

The start function only gets called once (at the beginning of the scene). Check the tutorial, try the code. It doesn't hurt.

avatar image dmg0600 · Sep 24, 2014 at 01:55 PM 7
Share

If you call it in Update it will be executed once every frame, as if it were a normal method call or an invoke.

If you want to be sure that it does not execute more than once until it finish its execution you can add a boolean flag:

 private bool isCoroutineExecuting = false;
 
 IEnumerator ExecuteAfterTime(float time)
 {
     if (isCoroutineExecuting)
         yield break;
 
     isCoroutineExecuting = true;
 
     yield return new WaitForSeconds(time);
  
     // Code to execute after the delay
 
     isCoroutineExecuting = false;
 }

This way it will stop the new coroutine if it is still executing a previous one and start if it is not executing it yet.

Show more comments
avatar image
62

Answer by 767_2 · Sep 24, 2014 at 01:51 PM

you can Invoke a method after a delay like this

  Invoke("DoSomething", 2);//this will happen after 2 seconds

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jan_Julius · Sep 24, 2014 at 01:55 PM 1
Share

This freezes my Unity.

avatar image 767_2 · Sep 24, 2014 at 02:02 PM 1
Share

if you put a function after invoke it will be executed immediately , why you say it freezes

avatar image 767_2 · Sep 24, 2014 at 02:32 PM 0
Share

why you say it freezes

avatar image Muskar · Mar 01, 2016 at 09:52 PM 5
Share

"DoSomething" is the name of the function to call. This answer is exactly what I needed, so I upvoted it, even though I know it wasn't the answer the OP was looking for.

avatar image shaneparsons · Jul 14, 2016 at 01:31 PM 1
Share

Same as what @$$anonymous$$uskar said – This isn't the answer the OP was looking for, but it's definitely what I was looking for!

Show more comments
avatar image
3

Answer by UberGeoff · Feb 01, 2018 at 02:08 AM

We can extend this futher by executing an Action after the delay:

 IEnumerator ExecuteAfterTime(float time, Action task)
 {
     if (isCoroutineExecuting)
         yield break;

     isCoroutineExecuting = true;

     yield return new WaitForSeconds(time);

     task();

     isCoroutineExecuting = false;
 }

Then use it like this:

  StartCoroutine(ExecuteAfterTime(0.5f, () =>
     {            
         //Add somwthing here
     }));
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KBVDev · Mar 29, 2018 at 10:09 AM 0
Share

What is the isCoroutineExecuting for? Im not sure if its doing anything

avatar image Legend_Bacon KBVDev · Mar 29, 2018 at 10:23 AM 0
Share

It just makes sure the coroutine isn't already waiting for "time", and breaks if it is. Because most of the time, you don't want multiple instances of the same coroutine running in parallel.

avatar image
3

Answer by TomZe · Sep 26, 2018 at 03:47 PM

use this class :

 using System.Collections;
 using System.Collections.Generic;
 using System.Reflection;
 using System.Linq;
 using UnityEngine;
 using System;
 
 namespace LateExe {
     class Executer
     {
         object script;
         MonoBehaviour mono_script;
         public Executer(object script)
         {
             this.script = script;
             this.mono_script = this.script as MonoBehaviour;
         }
         public InvokeId DelayExecute(float DelayInSeconds, Action<object[]> lambda, params object[] parameters)
         {
 
            return new InvokeId( mono_script.StartCoroutine(Delayed(DelayInSeconds, lambda, parameters)));
         }
         public InvokeId DelayExecute(float DelayInSeconds, string methodName, params object[] parameters)
         {
             foreach (MethodInfo method in script.GetType().GetMethods())
             {
                 if (method.Name == methodName)
                     return new InvokeId(mono_script.StartCoroutine(Delayed(DelayInSeconds, method, parameters)));
             }
             return null;
         }
         public InvokeId ConditionExecute(Func<bool> condition, string methodName, params object[] parameters)
         {
             foreach (MethodInfo method in script.GetType().GetMethods())
             {
                 if (method.Name == methodName)
                     return new InvokeId(mono_script.StartCoroutine(Delayed(condition, method, parameters)));
             }
             return null;
         }
         public InvokeId ConditionExecute(Func<bool> condition, Action<object[]> lambda, params object[] parameters)
         {
             return new InvokeId(mono_script.StartCoroutine(Delayed(condition, lambda, parameters)));
         }
 
         public void StopExecute(InvokeId id)
         {
             mono_script.StopCoroutine(id.coroutine);
         }
         IEnumerator Delayed(float DelayInSeconds, Action<object[]> lambda, params object[] parameters)
         {
             yield return new WaitForSeconds(DelayInSeconds);
             lambda.Invoke(parameters);
         }
         IEnumerator Delayed(float DelayInSeconds, MethodInfo method, params object[] parameters)
         {
             yield return new WaitForSeconds(DelayInSeconds);
             method.Invoke(script, parameters);
         }
         IEnumerator Delayed(Func<bool> condition, Action<object[]> lambda, params object[] parameters)
         {
             yield return new WaitUntil(condition);
             lambda.Invoke(parameters);
         }
         IEnumerator Delayed(Func<bool> condition, MethodInfo method, params object[] parameters)
         {
             yield return new WaitUntil(condition);
             method.Invoke(script, parameters);
             
         }
 
     }
     class InvokeId
     {
         public readonly Coroutine coroutine;
         public InvokeId(Coroutine coroutine)
         {
             this.coroutine = coroutine;
         }
     }
 }


Examples: execute after 2 seconds in a few ways

 using LateExe;
 
 public class Game_Script: MonoBehaviour {
 void Awake {
 Executer exe = new Executer(this);
 exe.DelayExecute(2f , x=> Debug.Log("after 2 seconds"));
 exe.DelayExecute(2f , x=> Debug.log(x[0]) , "after 2 seconds + params");
 exe.DelayExecute(2f , "after2seconds");
 exe.DelayExecute(2f , "afterXseconds", 2);
 exe.DelayExecute(2f , "afterXY", 2, " seconds");
 }
 void after2seconds ()
 {
 Debug.Log("after 2 seconds");
 }
 void afterXseconds(int x) {
 Debug.log("after" + x + "seconds");
 }
 void afterXY(int x , string y) {
 Debug.log("after " + x + y);
 }
 }

Example stop:

  var id = exe.DelayExecute(0.5f, x => Debug.Log("not gonna run"));
         exe.StopExecute(id);

Example when:

 exe.ConditionExecute(() => score > 100 , x => Debug.Log("you're good"));
         

I know this is old but its a very common thing and I hope unity will one day implement such a class into the engine. creating a coroutine every time is not a clean way of doing things.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image closingiris · Sep 29, 2020 at 01:08 PM 1
Share

I like this answer, but, tiny gripe --- should be Executor -- thanks for this tool btw, just finished implementing and it's very convenient and intuitive

avatar image
2

Answer by HarshadK · Sep 24, 2014 at 01:45 PM

Check Coroutines.

It is exactly what you need.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SaSha_K · Feb 06, 2017 at 03:11 PM 1
Share

Invoke() is simpler, but if one wants to call method with parameters, coroutine is apparently a good solution.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Have a delay after each jump, so user cant spam jump 3 Answers

Do something every 0.5 seconds 3 Answers

Animator idle minimal delay 0 Answers

Unity IAP InitializePurchasing taking too long. 1 Answer

Cycle Textures Over Time 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges