Run function next frame (Not in update()) UnityWhat is the difference between an abstract function and a virtual function?Collection was modified; enumeration operation may not executeHow do I update the GUI from another thread?Entity Framework 5 Updating a RecordKeeping Scores in Unity and pass to next sceneWhich is faster? (foreach loops and updates in Unity)Serial Communication Running Slow in UnityUnity resources folder and loading next texture on 3D objectHow to find game load time in Unity?Wait for some time in between onMouseDown() function in unity2D

Is this draw by repetition?

How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?

Where would I need my direct neural interface to be implanted?

One verb to replace 'be a member of' a club

How can saying a song's name be a copyright violation?

ssTTsSTtRrriinInnnnNNNIiinngg

Can someone clarify Hamming's notion of important problems in relation to modern academia?

how do we prove that a sum of two periods is still a period?

How to stretch the corners of this image so that it looks like a perfect rectangle?

Is there a hemisphere-neutral way of specifying a season?

Getting extremely large arrows with tikzcd

Was the Stack Exchange "Happy April Fools" page fitting with the '90's code?

How to prevent "they're falling in love" trope

How do conventional missiles fly?

Forgetting the musical notes while performing in concert

Can I hook these wires up to find the connection to a dead outlet?

Venezuelan girlfriend wants to travel the USA to be with me. What is the process?

Rotate ASCII Art by 45 Degrees

How obscure is the use of 令 in 令和?

files created then deleted at every second in tmp directory

What are the G forces leaving Earth orbit?

Is it possible to map the firing of neurons in the human brain so as to stimulate artificial memories in someone else?

Why were 5.25" floppy drives cheaper than 8"?

How to Prove P(a) → ∀x(P(x) ∨ ¬(x = a)) using Natural Deduction



Run function next frame (Not in update()) Unity


What is the difference between an abstract function and a virtual function?Collection was modified; enumeration operation may not executeHow do I update the GUI from another thread?Entity Framework 5 Updating a RecordKeeping Scores in Unity and pass to next sceneWhich is faster? (foreach loops and updates in Unity)Serial Communication Running Slow in UnityUnity resources folder and loading next texture on 3D objectHow to find game load time in Unity?Wait for some time in between onMouseDown() function in unity2D













0















I am trying to run a block of code a certain amount of seconds after a 2D Collision. Currently, when the collision occurs it calls a function that loops over itself until a certain time is reached. The time is calculated using a time variable and adding Time.deltaTime each iteration.



This is the function



public void springBack(Collider2D collision, float timeCount) 
if (timeCount > springReloadTime * 10)
Debug.Log("Springing Back!");

timeCount = 0f;
else
timeCount = timeCount + Time.deltaTime;

springBack(collision, timeCount);




The issue occurs when I run this. Everything happens in one frame right after the collision instead of after a few seconds. If I could call the springBack(collision, timeCount); function on the next frame, this would work. When I looked it up, some people suggested using yield return null;, but since this isn't a loop it gives me this error here:



The body of 'playerMovement.springBack(Collider2D, float)' cannot be an iterator block because 'void' is not an iterator interface type


Is there another way to force it to run on the next frame? I could call it from the update() loop, but I think I can have multiple instances of this function running without it inferring with each other. If you need the values of some of the public variables (like springReloadTime), please ask.










share|improve this question



















  • 1





    You can use yield only in coroutines (which is also what you want to use for this): docs.unity3d.com/Manual/Coroutines.html

    – UnholySheep
    Mar 21 at 20:50












  • @UnholySheep, Thanks. I've replaced the void with IEnumerator and added the yield return null; before the springBack(collision, timeCount); line and it ran without error, and completion. Any ideas?

    – Jack Stoller
    Mar 21 at 21:04






  • 1





    @JackStoller StartCoroutine(springBack()), springBack must return IEnumerator and inside this method implement your desired behaviour (it loks like you want to do it every n-th seconds, so you can do something like while(isLooping) springBack(); yield return new WaitForSeconds(time);. Be aware, that in that case you should store WaitForSeconds in property and then yield that property (var awaiter = new WaitForSeconds(n); .... while() yield return awaiter;). Using awaiter like this will not allocate memory everyTime, which is usefull especially in mobile development.

    – Pavel Pája Halbich
    Mar 21 at 21:33











  • @PavelPájaHalbich Thanks! Could you post an answer?

    – Jack Stoller
    Mar 21 at 21:59















0















I am trying to run a block of code a certain amount of seconds after a 2D Collision. Currently, when the collision occurs it calls a function that loops over itself until a certain time is reached. The time is calculated using a time variable and adding Time.deltaTime each iteration.



This is the function



public void springBack(Collider2D collision, float timeCount) 
if (timeCount > springReloadTime * 10)
Debug.Log("Springing Back!");

timeCount = 0f;
else
timeCount = timeCount + Time.deltaTime;

springBack(collision, timeCount);




The issue occurs when I run this. Everything happens in one frame right after the collision instead of after a few seconds. If I could call the springBack(collision, timeCount); function on the next frame, this would work. When I looked it up, some people suggested using yield return null;, but since this isn't a loop it gives me this error here:



The body of 'playerMovement.springBack(Collider2D, float)' cannot be an iterator block because 'void' is not an iterator interface type


Is there another way to force it to run on the next frame? I could call it from the update() loop, but I think I can have multiple instances of this function running without it inferring with each other. If you need the values of some of the public variables (like springReloadTime), please ask.










share|improve this question



















  • 1





    You can use yield only in coroutines (which is also what you want to use for this): docs.unity3d.com/Manual/Coroutines.html

    – UnholySheep
    Mar 21 at 20:50












  • @UnholySheep, Thanks. I've replaced the void with IEnumerator and added the yield return null; before the springBack(collision, timeCount); line and it ran without error, and completion. Any ideas?

    – Jack Stoller
    Mar 21 at 21:04






  • 1





    @JackStoller StartCoroutine(springBack()), springBack must return IEnumerator and inside this method implement your desired behaviour (it loks like you want to do it every n-th seconds, so you can do something like while(isLooping) springBack(); yield return new WaitForSeconds(time);. Be aware, that in that case you should store WaitForSeconds in property and then yield that property (var awaiter = new WaitForSeconds(n); .... while() yield return awaiter;). Using awaiter like this will not allocate memory everyTime, which is usefull especially in mobile development.

    – Pavel Pája Halbich
    Mar 21 at 21:33











  • @PavelPájaHalbich Thanks! Could you post an answer?

    – Jack Stoller
    Mar 21 at 21:59













0












0








0








I am trying to run a block of code a certain amount of seconds after a 2D Collision. Currently, when the collision occurs it calls a function that loops over itself until a certain time is reached. The time is calculated using a time variable and adding Time.deltaTime each iteration.



This is the function



public void springBack(Collider2D collision, float timeCount) 
if (timeCount > springReloadTime * 10)
Debug.Log("Springing Back!");

timeCount = 0f;
else
timeCount = timeCount + Time.deltaTime;

springBack(collision, timeCount);




The issue occurs when I run this. Everything happens in one frame right after the collision instead of after a few seconds. If I could call the springBack(collision, timeCount); function on the next frame, this would work. When I looked it up, some people suggested using yield return null;, but since this isn't a loop it gives me this error here:



The body of 'playerMovement.springBack(Collider2D, float)' cannot be an iterator block because 'void' is not an iterator interface type


Is there another way to force it to run on the next frame? I could call it from the update() loop, but I think I can have multiple instances of this function running without it inferring with each other. If you need the values of some of the public variables (like springReloadTime), please ask.










share|improve this question
















I am trying to run a block of code a certain amount of seconds after a 2D Collision. Currently, when the collision occurs it calls a function that loops over itself until a certain time is reached. The time is calculated using a time variable and adding Time.deltaTime each iteration.



This is the function



public void springBack(Collider2D collision, float timeCount) 
if (timeCount > springReloadTime * 10)
Debug.Log("Springing Back!");

timeCount = 0f;
else
timeCount = timeCount + Time.deltaTime;

springBack(collision, timeCount);




The issue occurs when I run this. Everything happens in one frame right after the collision instead of after a few seconds. If I could call the springBack(collision, timeCount); function on the next frame, this would work. When I looked it up, some people suggested using yield return null;, but since this isn't a loop it gives me this error here:



The body of 'playerMovement.springBack(Collider2D, float)' cannot be an iterator block because 'void' is not an iterator interface type


Is there another way to force it to run on the next frame? I could call it from the update() loop, but I think I can have multiple instances of this function running without it inferring with each other. If you need the values of some of the public variables (like springReloadTime), please ask.







c# unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 21:58









Nashical

231




231










asked Mar 21 at 20:33









Jack StollerJack Stoller

478313




478313







  • 1





    You can use yield only in coroutines (which is also what you want to use for this): docs.unity3d.com/Manual/Coroutines.html

    – UnholySheep
    Mar 21 at 20:50












  • @UnholySheep, Thanks. I've replaced the void with IEnumerator and added the yield return null; before the springBack(collision, timeCount); line and it ran without error, and completion. Any ideas?

    – Jack Stoller
    Mar 21 at 21:04






  • 1





    @JackStoller StartCoroutine(springBack()), springBack must return IEnumerator and inside this method implement your desired behaviour (it loks like you want to do it every n-th seconds, so you can do something like while(isLooping) springBack(); yield return new WaitForSeconds(time);. Be aware, that in that case you should store WaitForSeconds in property and then yield that property (var awaiter = new WaitForSeconds(n); .... while() yield return awaiter;). Using awaiter like this will not allocate memory everyTime, which is usefull especially in mobile development.

    – Pavel Pája Halbich
    Mar 21 at 21:33











  • @PavelPájaHalbich Thanks! Could you post an answer?

    – Jack Stoller
    Mar 21 at 21:59












  • 1





    You can use yield only in coroutines (which is also what you want to use for this): docs.unity3d.com/Manual/Coroutines.html

    – UnholySheep
    Mar 21 at 20:50












  • @UnholySheep, Thanks. I've replaced the void with IEnumerator and added the yield return null; before the springBack(collision, timeCount); line and it ran without error, and completion. Any ideas?

    – Jack Stoller
    Mar 21 at 21:04






  • 1





    @JackStoller StartCoroutine(springBack()), springBack must return IEnumerator and inside this method implement your desired behaviour (it loks like you want to do it every n-th seconds, so you can do something like while(isLooping) springBack(); yield return new WaitForSeconds(time);. Be aware, that in that case you should store WaitForSeconds in property and then yield that property (var awaiter = new WaitForSeconds(n); .... while() yield return awaiter;). Using awaiter like this will not allocate memory everyTime, which is usefull especially in mobile development.

    – Pavel Pája Halbich
    Mar 21 at 21:33











  • @PavelPájaHalbich Thanks! Could you post an answer?

    – Jack Stoller
    Mar 21 at 21:59







1




1





You can use yield only in coroutines (which is also what you want to use for this): docs.unity3d.com/Manual/Coroutines.html

– UnholySheep
Mar 21 at 20:50






You can use yield only in coroutines (which is also what you want to use for this): docs.unity3d.com/Manual/Coroutines.html

– UnholySheep
Mar 21 at 20:50














@UnholySheep, Thanks. I've replaced the void with IEnumerator and added the yield return null; before the springBack(collision, timeCount); line and it ran without error, and completion. Any ideas?

– Jack Stoller
Mar 21 at 21:04





@UnholySheep, Thanks. I've replaced the void with IEnumerator and added the yield return null; before the springBack(collision, timeCount); line and it ran without error, and completion. Any ideas?

– Jack Stoller
Mar 21 at 21:04




1




1





@JackStoller StartCoroutine(springBack()), springBack must return IEnumerator and inside this method implement your desired behaviour (it loks like you want to do it every n-th seconds, so you can do something like while(isLooping) springBack(); yield return new WaitForSeconds(time);. Be aware, that in that case you should store WaitForSeconds in property and then yield that property (var awaiter = new WaitForSeconds(n); .... while() yield return awaiter;). Using awaiter like this will not allocate memory everyTime, which is usefull especially in mobile development.

– Pavel Pája Halbich
Mar 21 at 21:33





@JackStoller StartCoroutine(springBack()), springBack must return IEnumerator and inside this method implement your desired behaviour (it loks like you want to do it every n-th seconds, so you can do something like while(isLooping) springBack(); yield return new WaitForSeconds(time);. Be aware, that in that case you should store WaitForSeconds in property and then yield that property (var awaiter = new WaitForSeconds(n); .... while() yield return awaiter;). Using awaiter like this will not allocate memory everyTime, which is usefull especially in mobile development.

– Pavel Pája Halbich
Mar 21 at 21:33













@PavelPájaHalbich Thanks! Could you post an answer?

– Jack Stoller
Mar 21 at 21:59





@PavelPájaHalbich Thanks! Could you post an answer?

– Jack Stoller
Mar 21 at 21:59












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288829%2frun-function-next-frame-not-in-update-unity%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288829%2frun-function-next-frame-not-in-update-unity%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript