- Home /
Starting a function as a coroutine in javascript using onclick via NGUI Button
So, in NGUI, you can use a Button script attached to a game object by dragging a game object with another script attached to it into the "notify" field of the NGUI Button. Then you can select a method in that script to call. I've written a script that needs a delay in the middle of it. So here's what I've written:
function enableWrongByTag()
{
for(var go : GameObject in GameObject.FindGameObjectsWithTag("Wrong"))
{
go.SetActive (true);
}
for(var stop : GameObject in GameObject.FindGameObjectsWithTag("None"))
{
stop.SetActive (false);
}
//insert delay somehow...
yield WaitForSeconds (5.0);
for(var stop : GameObject in GameObject.FindGameObjectsWithTag("None"))
{
stop.SetActive (true);
}
for(var go : GameObject in GameObject.FindGameObjectsWithTag("Wrong"))
{
go.SetActive (false);
}
for(var moo : GameObject in GameObject.FindGameObjectsWithTag("Correct"))
{
moo.SetActive (false);
}
}
Now I know that this function needs to be started as a coroutine in order to use the "yield WaitForSeconds(5.0)". But if I put the StartCoroutine(EnableWrongByTag()) BEFORE the actual script line, or in a function Start, then my function does not actually show up in the method list of the NGUI Button. So how can I run this script using an NGUI Button?
$$anonymous$$ake simple public method and StartCouroutine from that function.
Please forgive my noobness, but can you show me what you mean? Does the new method need to be in a separate file or can I just say something like:
function myCoroutine() { StartCoroutine(enableWrongByTag()); } // the rest of my function here...
Thanks for the speedy answer!
That's exactly what you have to do. But you will have to call myCoroutine() when the button is pressed ins$$anonymous$$d of enableWrongByTag().
Answer by Ratboy601 · Jul 13, 2014 at 06:09 PM
Ok, got it working, kind of. Thanks! I was just just testing it improperly. Turns out its something about the rest of my code that isn't working, although its not producing any errors when it compiles. But the yield works, and it shows up in my NGUI button so I'll see if I can work out the rest on my own now. Thanks again!