- Home /
How to access StartCoroutine in a static way
Hello, I got this small problem. I am using StartCoroutine for a www request but I can't figure a work-around to access it in a static way. Here is a sample code of what I do:
public static void mysql_query(string sServer, string sQuery)
{
sQuery = WWW.EscapeURL(sQuery);
www = new WWW(sServer+"?Query="+sQuery);
StartCoroutine(WaitForRequest(www));
if(www.error == null)
{
ResultSet = www.text;
}
else
{
Debug.Log("Error at: "+ www.error);
}
}
My problem is that StartCoroutine is obviously a non static method. I can't seem to figure out a work-around to access it in a none static way.
Answer by Tetrad · Oct 29, 2010 at 11:17 PM
Just give it a dummy gameobject to run on. Maybe put a script on that gameobject that gives you singleton-style access to it. You should then be able to do something like dummyComponent.StartCoroutine( whatever );
@cawas: This comment is pretty useless... Do you want to point out "roundabout (indirect)" isn't a proper word in this case?
@bunny83 finally unity $$anonymous$$m let me comment again (many migration issues with UDN I had)! nope, it's just useless indeed. shouldn't bother you so much, tho. you could say something funny back ins$$anonymous$$d. ;-)
Answer by cregox · Aug 14, 2013 at 04:20 PM
If you don't have at least 1 singleton in your whole project already, get one. And use it like Tetrad already said:
MySingleton.StartCoroutine( MyCoroutine);
Here's my favorite singleton implementation so far, I adapted from "50 tips for working with Unity" (currently broken link, so here's the google cache):
using UnityEngine;
/// <summary>
/// Be aware this will not prevent a non singleton constructor
/// such as `T myT = new T();`
/// To prevent that, add `protected T () {}` to your singleton class.
/// </summary>
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
private static object _lock = new object();
public static T Instance
{
get
{
if (applicationIsQuitting) {
Debug.LogWarning("[Singleton] Instance "+ typeof(T) +
" already destroyed on application quit." +
"Won't create again - returning null.");
return null;
}
lock(_lock)
{
if (_instance == null)
{
_instance = (T) FindObjectOfType(typeof(T));
if (_instance == null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent<T>();
singleton.name = "(singleton) "+ typeof(T).ToString();
DontDestroyOnLoad(singleton);
Debug.Log("[Singleton] An instance of " + typeof(T) +
" is needed in the scene, so '" + singleton +
"' was created with DontDestroyOnLoad.");
} else {
Debug.Log("[Singleton] Using instance already created: " +
_instance.gameObject.name);
}
}
return _instance;
}
}
}
private static bool applicationIsQuitting = false;
/// <summary>
/// When unity quits, it destroys objects in a random order.
/// In principle, a Singleton is only destroyed when application quits.
/// If any script calls Instance after it have been destroyed,
/// it will create a buggy ghost object that will stay on the Editor scene
/// even after stopping playing the Application. Really bad!
/// So, this was made to be sure we're not creating that buggy ghost object.
/// </summary>
public void OnDestroy () {
applicationIsQuitting = true;
}
}
I don't quite understand the usage of singletons.. I added the singleton script to my StandardAssets Folder, created a $$anonymous$$anager script as told and put my coroutine in there (called FadeOut), he gives me the following error:
"The body of $$anonymous$$anager.fadeOut(UnityEngine.UI.Text, float, UnityEngine.Color)' cannot be an iterator block because
void' is not an iterator interface type"
I have no idea what this error wants to tell me.. help!! And could you please tell me how to use singleton right, too?
Answer by brunoleos · Feb 17, 2017 at 09:12 AM
If you don't want a singleton (because your code can become very dependent on them, which makes it difficult to refactor) nor using dummy GameObjects (because you have to create/manage them), you can pass a MonoBehaviour as argument to your static method, and them call StartCoroutine from it.
If you call the static method from a script (Monobehaviour-based object attached to GameObject), you can call the static method using the "this" keyword:
Example static method:
public class Utility
{
public static void mysql_query(string sServer, string sQuery, MonoBehaviour justToStartCoroutine)
{
sQuery = WWW.EscapeURL(sQuery);
www = new WWW(sServer + "?Query=" + sQuery);
justToStartCoroutine.StartCoroutine(WaitForRequest(www));
if(www.error == null)
{
ResultSet = www.text;
}
else
{
Debug.Log("Error at: " + www.error);
}
}
}
Example call at YourScript.cs:
public class YourScript : MonoBehaviour
{
// Class members
void Start()
{
Utility.mysql_query("", "", this);
}
}