Null Checking in EditorWindow

So this is driving me insane.

  • I have a MonoBehaviour called ObjectA
  • Inside ObjectA is a custom list of ScriptableObjects: ObjectB (ListA of ObjectB)
  • Would like to keep ListA full of references, no nulls
  • User deletes ObjectB inside project, list now contains a null ref
  • On selection change in EditorWindow, validate ListA, removing any nulls

simple right?

well, I’m not sure if during the selection change event unity is some weird thread, but trying to check ListA with any nulls is impossible. When logging the items I see the objects are null. But trying to determine whether that object actually is null isn’t working. e.g:

Debug.Log(obj) //null
if (obj == null) //always false

I’ve tried:

if (ReferenceEquals(obj, null)) //false

T item = list ?? default(T)
if (item == null) //false
I can’t get the type because the object is null! I get a null ref when trying to access. What is going on here? I’m going crazy

in the editor debug, the field says “Missing”

is there a way to check for that?

ok after some googling, my problem is “How to detect a missing object reference”

apparently that’s different than a null reference. Here’s the solution:

public void RemoveNullRefs() {

    int i = list.Length;

    while (--i > -1) {

        try {

            string s = list[i].ToString();

            if (s.ToLower() == "null") {

                RemoveAt(i);
            }
        }
        catch {

            RemoveAt(i);
        }
    }
}