- Home /
Call StartCoroutine() from inside a function other than Start!?
Dear, I have a class DBConn, which have 2 functions JoystickFound() and GetData():
public void JoystickFound(string JoystickName)
{
print ("Qr Found : "+JoystickName);
StartCoroutine(GetData(JoystickName));
}
IEnumerator GetData(string JoystickName)
{
WWWForm sendJoystickInfo = new WWWForm();
sendJoystickInfo.AddField("DBJoystickName",JoystickName);
WWW getdata = new WWW(GetUrlsPhp,sendJoystickInfo );
yield return getdata;
string[] split = getdata.text.Split("|"[0]);
JoystickName = split[0];
ProfileName = split[1];
LevellName = split[2];
}
When I run the project, i see an error that the StartCoroutine function should be called from Start function, when I do it it works perfectly. However, I need to be able to run this function from inside a function Joystick Found, and not from start, any idea how to do that?
I edited your code for you this time. Use the 101/010 button when posting code in the future, and look at the preview of what your post is going to look like before you submit it. If it looks jacked, format it properly.
Answer by Eric5h5 · Mar 31, 2014 at 08:13 PM
You can use StartCoroutine in any function, as long as the class derives from MonoBehaviour.
How can i do that?? If i upload the project to you, can you help me with it?
Answer by Bunny83 · Mar 31, 2014 at 08:29 PM
There is no requirement that StartCoroutine has to be called from Start, but it has to be called from the main thread. So it depends who's calling the JoystickFound method. If it's some kind of callback from a plugin it's probably invoked from another thread. In this case you might want to use something like the Loom class at the bottom of the article to schedule the method on the mainthread.
There's no way around this. The whole Unity API is not thread-safe and does only work when used from the main thread.
ps: without seeing the whole picture we can't say where you did something wrong.
If i upload the project to you, can you help me with it?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player lives script help 1 Answer
Trigger GUI with a key when colliding (C#)? 1 Answer
Pause Game When Function Is Enabled? 3 Answers