Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to call a function next frame?

Discussion in 'Scripting' started by tansvanio, Feb 12, 2016.

  1. tansvanio

    tansvanio

    Joined:
    Dec 16, 2012
    Posts:
    12
    Greetings

    I am having a problem due to a gameobject not updating its childCount fast enouph, this happens becouse functionA changes the childCount of that gameobject and functionB uses that childCount to operate.

    I aim to fix this by calling functionB one frame later, becouse the childCount should be updated by then.

    Lets say I have this code:

    Code (CSharp):
    1. if(condition)
    2. {
    3.     functionA(argument1);
    4.     functionB(argument2);
    5. }

    How can I call functionB one frame later after I enter that if statement? (By calling one frame later, I mean "delaying" its call by one frame.)

    Many thanks
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2. if(condition){
    3. functionB(argument2);
    4. }
    5.  
    6. condition = true;
    7.  
    8. if(condition){
    9. functionA(argument1);
    10. }
    11.  
    12.  
     
  3. tansvanio

    tansvanio

    Joined:
    Dec 16, 2012
    Posts:
    12
    Thanks for answering, however that did not work. I think that block of code is executed in a single frame aswell.
     
  4. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40
    add a private bool triggerFunctionB; as a member variable. Then in function A set that variable to true. In function B turn that variable to false. In Update() add if(triggerFunctionB) {functionB(argument2);} Make sure to turn the triggerFunctionB to false or it will happen every frame.
     
  5. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    yield WaitForSecons(0);

    Code (JavaScript):
    1. if(condition)
    2. {
    3.     functionA(argument1);
    4.     yield WaitForSeconds(0);
    5.     functionB(argument2);
    6. }
     
    MertYavuz81 likes this.