- Home /
An object reference is required to access non-static member
so i have this coroutine i want to start. but the fucntion i start it from has to be static.
and then it throws me this error. What is the right way to fix this?
heres my code:
public class remoteDebug : MonoBehaviour {
static string Debug_URL = "http://www.url.com/submitDebug.php";
public static void log (string text)
{
StartCoroutine(handleLog(text));
}
static IEnumerator handleLog(string info)
{
string send_url = Debug_URL + "info=" + info;
WWW loginReader = new WWW(send_url);
yield return loginReader;
Debug.Log (loginReader.text);
if (loginReader.error != null)
{
//good
}
else
{
//bad
}
}
}
if i make handleLog static it still gives the same error.
Oh yes, since in a static method you are calling StartCoroutine, which is a member of $$anonymous$$onoBehaviour and is an instance method which is why the error is being raised. The only option is to remove the static keyword from the log function.
Answer by Baste · Jan 14, 2015 at 12:26 PM
Static coroutines doesn't work out of the box in Unity. This is because coroutines are essentially an extra piece of code attached to a script component, and needs that script component to be called again after a yield has returned.
To solve this, you have some options:
1: make the remoteDebug a singleton monoBehaviour, and attach the coroutines to it.
2: Implement static coroutines.
3: Work around it by regularly polling a bool variable to check if the static method you're waiting for is finished.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180405im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Audio mixer Setfloat() method problem in coroutine 0 Answers
How to debug a coroutine? 2 Answers