Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
4
Question by ForgeStudios · Mar 17, 2015 at 12:55 PM · c#monobehaviour

C# Invoke Function with Params and Delay

Is there a concise way to call the invoke function on a void with parameters, on a delay. I understand that you can call:

 Invoke(string function, float delay)

However, that's not all too helpful when your function has parameter(s)

 void writeToConsole(string text){
 

his is does.

Comment
Add comment
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

3 Replies

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

Answer by ThomasVandenberghe · Mar 17, 2015 at 02:35 PM

You used to be able to if I remember correctly, but I can't find it in the docs. So my advice would be to use a Coroutine instead. It works kind of the same way.

 StartCoroutine(Foo("Text", 2));
 
 IEnumerator Foo(string text, float delay)
 {
     yield return new WaitForSeconds(delay);
 
     // code here
 }
Comment
Add comment · 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
0

Answer by JacobGames · Feb 17, 2016 at 05:00 PM

@ForgeStudios we have created a paid asset that can help you delay functions with parameters in just one line of code with no need to use coroutines.

It's called Super Invoke, it's like a more powerful Invoke.

Using Super Invoke your code would become:

 float delay = 1f;
 SuperInvoke.Run( ()=> writeToConsole("A delayed log"), delay);


You can also create sequences with methods and delays with which you can fully control a complex flow in a handy and easy-to-read way.

For instance you might want to write something to the console and after 1 second you want to instantiate an object. With Super Invoke it will be:

 SuperInvoke.CreateSequence()
     .AddMethod( ()=> writeToConsole("Hello World"))
     .AddDelay(1f)
     .AddMethod( ()=> Instantiate(object))
     .Run();
Comment
Add comment · Show 3 · 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 Bunny83 · Feb 17, 2016 at 06:09 PM 1
Share

That looks a bit like my (free) CoroutineHelper ;)

btw: your examples are missing a closing bracket in line 2 and 4 at the end

avatar image JacobGames Bunny83 · Feb 18, 2016 at 10:29 AM 1
Share

@Bunny83 thank you, missed those brackets.

CoroutineHelper looks great.

We're already working on further improvements of Super Invoke in which we'll add a repeat feature with ti$$anonymous$$gs and the choice of how many times the delayed call should be executed.

A useful additional feature would be a callback system.

Apart functionalities, perhaps one crucial component for a third party asset is the management of garbage. We are working on an internal pooling system which could potentially bring the run time allocation to 0 B.

In CoroutineHelper each method creates runtime allocations.

Testing the Run.After method:

The first time Run.After is called it allocates 3.2 $$anonymous$$B due to the creation of the CoroutineHelper GameObject, it then allocates 464 B each frame probably due to the usage of the OnGUI method in the coroutine script (this allocation was not present before Run.After was called for the first time).

Subsequent calls of Run.After allocate 173 B.

SuperInvoke creates runtime allocations only when it is actually used, as for now it is between 50 B and 100 B, it optimizes some calls but the new pooling system will bring it close to 0 B.

We use Super Invoke for our own projects. The sequence feature helped us simplifying our work in many different ways. We thought it could be helpful to other people too.

avatar image Bunny83 JacobGames · Feb 18, 2016 at 02:18 PM 1
Share

Of course. It was never ment to be optimised for low garbage creation. Yes, the OnGUI thing could be optimised with a seperate script for those if needed.

The allocation for each Run.After call is expected and needed. Since the user can store and use the returned "Run" object I can't pool it and re-use it.

Also the creation of anonymous methods / lambda expression also creates garbage. It's even worse when you create a closure like in this case:

 .Add$$anonymous$$ethod( ()=> Instantiate(object))
avatar image
0

Answer by SimoGecko · Sep 27, 2020 at 04:25 PM

You can find how to do that here https://forum.unity.com/threads/tip-invoke-any-function-with-delay-also-with-parameters.978273/

Comment
Add comment · 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

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

"The class named is not derived from MonoBehaviour or ScriptableObject!", although class is derived from MonoBehaviour 2 Answers

Update and Awake not being called. 1 Answer

Can you have multiple MonoBehaviors in the same script file? 1 Answer


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