gameObject.AddComponent() is returning null

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?

MonoBehaviour-derived classes are required to be instantiated via AddComponent() by design. Components only exist within the scope of a GameObject, and AddComponent() is what hooks up all the dependencies between the two.

I’m not certain what’s going on in this case; I’m surprised you get a null value and no error message. But I would guess the problem is that Getter is declared ‘internal’. Try changing it to public instead. It that doesn’t work, I’m at a loss.

I tried making it public but that didn’t make any difference, nor did removing the ILoadable interface. I got it to work though and your comment put me on the right track. I had this Getter class inside a namespace, once I removed it and put it in its own .cs file everything worked. I guess Classes derived from MonoBehaviour can’t be in a namespace?

No, unfortunately Unity doesn’t support namespaces for MonoBehaviours.

I’m trying to call AddComponent to a class that is derived from another that extends MonoBehaviour…
All i got is a empty log message on Console :

UnityEngine.GameObject:AddComponent()

and the gameObject.AddComponent() returning null.

EDIT:

I’ve found the problem: the file name was different from the class name (a duplicated letter), and Unity wasn’t warning me of that (it usually does and don’t even let the game run on editor).

I also faced a similar problem , I was trying to use AddComponent method in the Start method , I realized that it should be called in Awake method. This fixed the issue for me . I hope it helps others .

I’ve never seen any issues calling AddComponent in Start. Probably something else in your code.

–Eric