Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DagnarokCoding · Jul 19, 2016 at 03:27 AM · c#scripting problemgamecoroutineinvoke

C# | How to delay a method with parameters

I got a line of code that is basicly a component from inside this main component, calling a method from that component while passing a parameter from this component. Here:

 posCard place = DropZone.GetComponent<posCard>();
                 place.AddCardEnemy (card);

Now, how to delay this line, if Invoke only takes methods without params and Coroutine needs an IEnum?

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

2 Replies

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

Answer by jdean300 · Jul 19, 2016 at 04:02 AM

Create a new coroutine method that waits a given amount of time and then calls the method:

 public IEnumerator AddCardEnemyDelayed(float t, posCard place, Card card)
 {
     yield return new WaitForSeconds(t);
     place.AddCardEnemy(card);
 }
 
 //This can then be called by:
 StartCoroutine(AddCardEnemyDelayed(1f, place, card));
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 Nodata · Jun 24, 2020 at 04:59 PM

This class will allow you to extend any monobehaviour and run delayed coroutines with parameters:

 public static class MonoBehaviourExt
 {
     public static IEnumerator DelayedCoroutine(this MonoBehaviour mb, float delay, System.Action a)
     {
         yield return new WaitForSeconds(delay);
         a();
     }
 
     public static Coroutine RunDelayed(this MonoBehaviour mb, float delay, System.Action a)
     {
         return mb.StartCoroutine(mb.DelayedCoroutine(delay, a));
     }
 
     //Example
     //public class Buff: MonoBehaviour
     //{
     //  public void OnBuff()
     //  {
     //      StopAllCoroutines();
     //      Debug.Log("buffed");
     //      this.RunDelayed(2f,() => {
     //          Debug.Log("Debuffed");
     //      });
     //  }
     //}
 }



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 Bunny83 · Jun 24, 2020 at 05:16 PM 0
Share

You may want to have a look at my CoroutineHelper. It provides several static methods which allow to schedule delegates at different times / conditions. It starts the coroutines on a seperate monobehaviour singleton.

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

7 People are following this question.

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

Related Questions

Function delay? 1 Answer

Light problem 1 Answer

WaitForSeconds is not waiting for seconds, at all. Its like it doesnt exist. 1 Answer

how do i use component gathered from ontriggerEnter, outside of that function? 2 Answers

Coroutines randomly stop working 0 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