- Home /
Call another function during wait for seconds
I have a code like below
StartCoroutine("MyFunction");
function MyFunction() { //Some Line of codes Yield WaitForSeconds(5); //Rest of codes }
But during that WaitForSeconds user may press another button which cause another function call. my problem is that when the other function is called, codes after the WaitForSeconds command do not run. Can anyone help me to know what the problem is? and also the solution!
Can't help you without "Rest of codes" and "other function code". $$anonymous$$ost likely in your "other function code" you change some value or remove some object which is needed in "Rest of codes". You should revise logic to prevent that (e.g. add some boolean value $$anonymous$$yFunctionLaunched and set it to true before WaitForSeconds, then set it to false after the "Rest of codes" and in your "other function code" check if $$anonymous$$yFunctionLaunched is true before you change that critical data, or something like that). Anyway if you need further assistance provide code.
What about creating custom events and subscribe to them, have you tried that? Without code is hard to tell
Oh I see in the other function I made deactivate the object which main script is attached to, which cause the script stopped.
Thanks for answering.
so we can help you in future if you need help from us next time
Answer by JigneshKoradiya · May 20, 2015 at 05:51 PM
function Start()
{
StartCoroutine("MyFunction");
}
function MyFunction()
{
Yield WaitForSeconds(5); Debug.Log("After 5 second called");
}
function ButtonEvent()
{
StopCoroutine("MyFunction");
Debug.Log("Button Clicked");
}
Your answer
Follow this Question
Related Questions
WaitForSeconds not waiting 1 Answer
Coroutines with Yield return new waitforseconds(); go for execution of other functions? 0 Answers
yield, Coroutines, InvokeRepeating... 1 Answer
WaitForSeconds not working in IEnumerator function using MoveNext 1 Answer
How to add a small delay to script? 0 Answers