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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?