Variables in a Class Act as if it's Null

I have an issue that must be because I am overlooking something simple. I wanted to create a class so that I can organize variables in the my script better. However, I am having an issue with accessing an object within it.

In this example I boiled it down as simple as I could. I created a c# class called NullIssue and inside of that same file I created a class called TestClass. I made TestClass serializable so I could see it in the editor. The only thing it does is print the object in the debug log.

So I attached it to a game object and dragged another game object into the slot for the class’ ‘go’ object inside of the inspector, ran the code, and the debug log reads “NULL”.

Anyone have any ideas of what is going wrong here? I left the code in its entirety below.

P.S. Inside of the inspector, when I run it, the ‘go’ field still has the game object inside of it.

using UnityEngine;
using System.Collections;

public class NullIssue : MonoBehaviour {

    public TestClass tc = new TestClass();

}

[System.Serializable]
public class TestClass
{
    public GameObject go;

    public TestClass()
    {
        Debug.Log(go);
    }
}

I’m not too sure, but try not instantiating the TestClass; Unity might be doing that for you.

public TestClass tc;

Also, might as well keep TestClass in its own dedicated script file.

That didn’t work either. I split up the two into their own script files, removed the code for instantiating, played with inheriting MonoBehaviour to TestClass. And basically different combinations of those things and so far nothing has worked.

I tried to do a line like “if(go)” to see if that would even have it skip over the code, but I get an error that CompareBaseObjects can only be called from the main thread. Could this be a related issue that something I am doing is happening outside of the main thread which is causing the system to null that variable?

Anyone have any ideas or other ways of organizing something like a class of variables and functions? I can hard code the things in the script to get it to work, but that would get messy fast. If anyone is doing this same thing in C# and it’s working, let me know. Maybe it is a bug with the current build of 3.0

Hey there, I’m having a similar problem in Unity v3. I created a simple singleton from the example here…

I attach the script to a simple gameObject and I am getting the exact same error when I run from MonoDevelop (v2.4) my CPU spikes to 95% and I get the same error on the following IF line below:

public class MySingleton
{
    private static MySingleton instance;
   
    public MySingleton ()
    {
        if (instance != null) // <<<============ THIS LINE IS GENERATING THE ERROR.
        {
            Debug.LogError ("Cannot have two instances of singleton. Self destruction in 3...");
            return;
        }

I even tried tried moving the code from the constructor into an Awake() function but am getting the same results. Even shows the error in the inspector in MonoDevelop.

Any help would be greatly appreciated!
Thanks, Kris

OK, I tried something that worked. I moved the code from the constructor into the Awake() method AND changed the code to NOT reference the instance. Instead I check a local class variable to see if it is null and (if not) then call the init method. I tried it in MonoDevelop (debugging) and directly in Unity and each worked without a hitch or error. See code changes below. Hope this helps someone.

public class NetworkController : MonoBehaviour
{
	private static NetworkController instance;
	private ArrayList listeners;
	//private int timer;
	
	//public NetworkController() 
	public void Awake()
	{
//		if (instance != null)
//		{
//			Debug.LogError ("Cannot have two instances of singleton NetworkController. Self destruction in 3, 2, 1...BOOM!");
//			return;
//		}

		if (listeners == null)
		{
			instance = this;
			Init();
		}
	}

Thanks,
Kris