I am about to lose my mind. For some crazy reason I cant instantiate classes derived from MonoBehaviours without using AddComponent. Ok I’ll accept that I guess. But why in the hell would AddComponent return a null reference with my class?
Here is the code which “instantiates” the class
GameObject getObject = new GameObject();
Getter getXml = getObject.AddComponent<Getter>();
Debug.Log("is getXml null? " + (getXml == null));
getXml.LoadCompleteEvent += processXml;
getXml.load(url);
Here is the Getter class…
internal class Getter : MonoBehaviour, ILoadable
{
private LoadEventArguments arguments;
public event LoadCompleteHandler LoadCompleteEvent;
public void OnLoadComplete(object sender, LoadEventArguments e)
{
LoadCompleteEvent(sender, e);
}
public void Awake()
{
Debug.Log("it started");
}
public void loadImage(string url, Texture2D tex)
{
arguments = new LoadEventArguments();
WWW www = new WWW(url);
arguments.url = url;
arguments.www = www;
arguments.content = tex;
StartCoroutine(waitForLoad(www));
}
public void load(string url)
{
WWW www = new WWW(url);
arguments.url = url;
arguments.www = www;
StartCoroutine(waitForLoad(www));
}
IEnumerator waitForLoad(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
OnLoadComplete(this, arguments);
}
else
{
Debug.Log("WWW Error: " + www.error);
}
}
}
Can anyone figure this out? Also, is there a better way of monitoring the WWW class for completion other than this coroutine method, something that doesn’t involve Monobehaviour perhaps?