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
0
Question by InsertFunnyUsernameHere · May 15, 2015 at 05:39 PM · coroutineadvanced

Suspension function

So basicaly I was wondering if there's a way to write a function suspending the execution of another funtion, for example

 void Foo()
 {
 // do something
 }
 
 Suspend(Foo,5); //Foo() will be executed after 5 seconds


So something like the following...except it doesn't work :)

 public class SuspendScript<T> : Monobehaviour
 {
  public delegate void MyDelegate(T parameter);
  public MyDelegate functionToSuspend;
  public T parameter; // the parameter of the suspended function
 
 public void Suspend(float time)
 {
  StartCoroutine(SuspensionCoroutine(time));
 } 
 
 IEnumerable SuspensionCoroutine(float time)
 {
 yield return new WaitForSeconds(time);
 functionToSuspend(parameter);// execute suspended function after *time* seconds
 }
 }
 


and now the calling function

       public class SomeClass : MonoBehaviour
         {
         SuspendScript<string> SS;
         void JustForGigs(string s)
         {
         Debug.Log(s);
         }
         
         void Start()
         {
         SS=new SuspendScript<string>();
         SS.functionToSuspend=JustForGigs;
         SS.parameter="Haha!";
     SS.Suspend(5); //JustForGigs will be executed after 5 seconds
         }


Expected outcome: Debug.Log("Haha!")

Actual Outcome: NullReferenceException :(

yeah so my question is if it's even possible to suspend a function like this? And if it is indeed possible then what am I doing wrong? Is there a better way?

Comment
Add comment · Show 2
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 FortisVenaliter · May 15, 2015 at 05:43 PM 0
Share

One thing I notice is that you are using a $$anonymous$$onoBehaviour, but you can't instantiate $$anonymous$$onoBehaviours with "new", you have to use AddComponent(). It should be throwing you an error for that as well.

Have you tried deriving your SuspendScript class from ScriptableObject ins$$anonymous$$d?

avatar image InsertFunnyUsernameHere · May 15, 2015 at 06:09 PM 0
Share

You sir are a genius! Thank you so much!

2 Replies

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

Answer by InsertFunnyUsernameHere · May 15, 2015 at 06:17 PM

Yeah so FortisVentalier gave me the answer I needed and I managed to create a working suspension script, check it out

 public class SuspensionScript<T> 
 {
 
     public delegate void MyDelegate(T value);
     public MyDelegate function; //the function to suspend
     public T param; // the parameter of the function to suspend
 
 
     public IEnumerator SuspensionCoroutine(float time)
     {
         Debug.Log ("MyCoroutine");
         yield return new WaitForSeconds (time);
         function (param);
         yield return null;
     }
 
 }


caller function: (you may use any datatype instead of string, just put it in the <> )

     public class SomeClass : MonoBehavior
     {
     
     SuspensionScript<string> SS;
     
     void JustForGigs(string s)
     {
     Debug.Log(s);
     }
     
     void Start()
     {
     SS = new SuspensionScript<string>();
     SS.function = JustForGigs;
     SS.param="Haha!";
 StartCoroutine(SS.SuspensionCoroutine(5)); //JustForGigs will be executed after 5 seconds
     }
     
     }
 

Have fun making Time Bombs and stuff

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
1

Answer by cjdev · May 15, 2015 at 07:45 PM

Another way of doing it could be using a simple function in the same class, just depends on your needs:

 using UnityEngine;
 using UnityEditor.VersionControl;
 using System.Collections;
 using System;
 
 public class Coroutines : MonoBehaviour {
 
     void Start () {
         StartCoroutine(Suspend((s) => JustForGigs("Haha!"), 5f));
     }
 
     public void JustForGigs(string s)
     {
         Debug.Log(s);
     }
     
     public IEnumerator Suspend(Action<Message> d, float time)
     {
         yield return new WaitForSeconds(time);
         d.Invoke(new Message());
     }
 }


The Action delegate is in the form of (random parameter variables) => FunctionName(actual parameters).

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 InsertFunnyUsernameHere · May 15, 2015 at 08:04 PM 0
Share

whoa that's too smart for me. I actualy tried to make it fit into one script but I just couldn't no matter how I tried. Gonnah learn lambda functions now, ty

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

20 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

Related Questions

StartCoroutine doesn't work for me to remove momentary unresponsive when loading OBJ file at runtime. Multithreading is a good way to go? any help? 1 Answer

WWW.Dispose & coroutine 0 Answers

my co-routine isn't running when i tell it to 0 Answers

Why do multiple Unity coroutines speed up www image loading? 1 Answer

Rotate an object using Quaternion.RotateTowards() inside a Coroutine? 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