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
2
Question by Fattie · Nov 13, 2012 at 08:30 AM · yieldcoroutinesinvoke

How to Invoke() .. one frame

Unityscript ...

I am in routine BB and I want routine CC to run, in the next frame. The elegant solution would look something like..

 Invoke( "CC", one frame );

Is there such a thing? If you try Invoke( "CC", 0.00001 ) does that in fact safely mean next frame -- investigation says NO, that does not work. Perhaps some undocumented parameter?

PS I am fully aware you can do this by calling DD where DD is

 function DD() { yield; CC(); }

but then that's a coroutine, blah blah. (Come to think of it, I guess you could make a InvokeNextFrame(F:Function){yield;F();} But it's not elegant or builtin.)

Anyway does anyone know the elegant way to "Invoke next frame" ? Thanks a million.


Exactly as my copain below says, in unityscriot, you can test like this:

 private var index:int = 0;
 function Update()
 {
 if ( ++index > 10) return;
 Debug.Log("runloop "+index);
 Invoke("Print",0.00001);
 }
 function Print()
 { Debug.Log("printer "+index); }

You will see it does not work, not reliably anyway.


By the way, here's a demo that the inelegant "call a caller" function works as far as it goes.

 private var index:int = 0;
 function Update()
 {
 if ( ++index>10) return;
 Debug.Log("in loop "+index);
 INFPrint();
 }
 function Print()
 {Debug.Log("printer "+index);}
 function INFPrint() { yield; Print(); }
 // INF means Invoke Next Frame

same deal for this InvokeNextFrame(F:Function){yield;F();

Maybe it's really better just to have your own runloop going :-/

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
1
Best Answer

Answer by fafase · Nov 13, 2012 at 08:59 AM

OK I run a little test, maybe that will help you out:

 public class Test : MonoBehaviour {
     int index = 0;
     void Update () {
         index++;
         if(Input.GetKeyDown(KeyCode.Space)){
             print (index);
             Invoke ("Print",0.0001f);
         }
     }
     void Print(){
         print (index);
     }
 }

As you can see the index is incremented each frame. Running this I can see the printing value are the same inside the Update and in the function invoked.

Now if I change the value from 0.0001f to 1 I see total different values.

Now if I use:

 Invoke ("Print",Time.deltaTime);

I see a +1 meaning next frame. Problem is that deltaTime is not really reliable as a constant.

Is that what you needed?

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 Fattie · Nov 13, 2012 at 09:16 AM 0
Share

Thanks for spending all the time on this --- right, that's exactly how you would test it. Interestingly in your test you report the "small value trick" DOES NOT work (I've had the opposite result -- actually I just tested it now and like you I found it does not work).

I think Invoking on Time.deltaTime just seems like a horrific idea, I'm sure you agree - it can't possibly work every time.

We just need someone who absolutely knows if there is an undocumented way to do this :-/

Thanks again ...

avatar image fafase · Nov 13, 2012 at 09:22 AM 0
Share

the deltaTime thingy would fix it in the FixedUpdate but in Update I would not risk it...

avatar image Fattie · Nov 17, 2012 at 07:06 AM 0
Share

no really good solution on this, I've been using this

 InvokeNextFrame(F:Function)
 {
 yield;
 F();
 }

around. Sometimes you need to StartCoroutine on the F() of course. It's a mess. I can't believe youc an't just schedule next frame. Thanks for the time.

Os see $$anonymous$$ichael's tidy version below

avatar image
7

Answer by MichaelTaylor3d · Nov 30, 2012 at 05:02 PM

Here is a solution that wont require you to use a coroutine in your current class, just put this in your base class.

     public delegate void Function();
         
     public void InvokeNextFrame(Function function)
     {
         try
         {
             StartCoroutine(_InvokeNextFrame(function));    
         }
         catch
         {
             Debug.Log ("Trying to invoke " + function.ToString() + " but it doesnt seem to exist");    
         }            
     }
     
     private IEnumerator _InvokeNextFrame(Function function)
     {
         yield return null;
         function();
     }
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 Fattie · Nov 30, 2012 at 07:58 PM 0
Share

good one..

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

12 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

Related Questions

Is it possible to invoke a button Press With a Coroutine? 1 Answer

Invoke Repeating Rate 1 Answer

Coroutine not running 1 Answer

Yield return inside a loop slowdown problem 2 Answers

WaitUntil doesn't work and the coroutine starts anyways 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