Have Unity Debug.Error printout exact NullReferenceException object it's printing?

The title says it all. I was wondering if there is a way to have Unity’s Debug.Error to printout what object is null. I know it is helpful already with giving me the line and file and such, but sometimes on some lines there are a multitude of objects I’m working with, and it would just be more productive for me overall to be able to get directly to the root of the problem.

Does anyone know a way to get that information in C# or through extending Unity Debug.Error?

Perhaps you could try using a function to verify the GameObject exists before calling your statement. Technically, before using Any data, to prevent nullrefs you should always verify the data isn’t null. If it is, you need to skip the code you would otherwise have executed and generally end the function;

public bool objectExists(String theGameObject){

try
{
GameObject thisObject = GameObject.Find(theGameObject);
if(thisObject != null){
return true;
}else{
return false;
}
}catch(Exception ex){
Debug.Log(ex);
return false;
}
}

There’s nothing worse then having client input capable of being able to crash a server. And generally you want to avoid any forms of crashing what so ever with C# =)

Hey sarmth,

I do almost always check for null, except in rare cases, but as is the case with Unity and any game related projects, sometimes I have to work with others code, or random voodoo magic happens, and it was more of a question I posed in terms of saving me time having to Debug and guess what object may be null and then going through and making the proper checks.

I figured if the Debug told me the line and object that is null, that would save me more time, though I’m not sure if that’s possible.

I’m guessing that the version of Mono that Unity uses simply doesn’t support column numbers in the symbol files. They’re either not being added because required compiler features are disabled, or the database simply doesn’t support the data. Otherwise there wouldn’t really be any reason for the client to withhold that information when handling an exception.