- Home /
NullReferenceException yet Debug.Log shows nothing is null?
I'm getting a Null Reference Exception on a snippet of code, yet I can't for the life of me figure out why the variable would be null. Here's the problematic function:
public void Damage (HEntity.EntityDamage v)
{
v.amount *= data.damageMod;
v.isCritical = data.isCritical;
v.hitboxCritical = data.criticalMul;
if (entity)
entity.Damage(v);
}
The NullReference error happens on v.amount *= data.damageMod.
I've already changed data.damageMod to 2.0f just to be sure it was v that was causing the issue. The error still happened, so it must be a problem with v.
This is how the v gets created and passed in (in a different script):
HEntity.EntityDamage damage = new HEntity.EntityDamage();
damage.amount = stats.baseDamage;
damage.weaponIgnoreCritical = !stats.hasHeadshot;
damage.criticalMul = stats.headshotMul;
col.Damage(damage);
HEntity.EntityDamage is both public and has the System.Serializable attribute.
When I comment out the line v.amount *= data.damageMod, the error shifts to v.isCritical. This leads me to believe the problem lies with v itself and not any fields contained, unless both fields are null. However, that isn't the case. I ran:
Debug.Log(v == null);
Within the problematic function and it returned false. I also ran:
Debug.Log(v.amount == null);
And it returned false. I also checked:
Debug.Log(v.amount);
And it printed the expected value of 30 for the test case I was running.
All fields in HEntity.EntityDamage have default values in the definition, and v.amount is clearly being set by the other script. Is there some reason why my current script can't write data from a class that was created somewhere else? This problem didn't happen in UnityScript, and I don't see why that would happen here.
I've even gone as far as to try:
v.amount = new float();
v.amount = 20.0f;
Upon creation of the class that still didn't work. Any idea what could be wrong?
Thanks in advance!
"I've already changed data.damage$$anonymous$$od to 2.0f just to be sure it was v that was causing the "
But did you verify that the null exception you got during that experiment was NOT from the next line? Put another way, did you verify that data is not null the way you did with v?
What is your development platform and C# editing tool? If you use Visual Studio under Windows you can debug this code much more easily and see all that is happening without using debug output lines like this (it's much, much faster - and in more complex word that's much to the 5th power or higher).
Answer by SomeGuy22 · Aug 29, 2018 at 10:17 PM
@JVene was right--I didn't check the new error's details after removing the data variable from that line. It actually shifted to a different line, and found that data was == null. What's odd is that I did check if data.damageMod was null and that returns false for some reason, yet data's check returns true.
I used Debug.Log's object link (the second parameter) to track down which object in the hierarchy was causing the issue, and found that a different object entirely was catching the Raycast and the function was being called there. I still found it odd that data was returning null since I expected it to be serialized within the Inspector, until I realized that the object was Instantiated at runtime, and I had added the script using AddComponent. I suppose if you AddComponent you need to Initialize all it's variables, which I thought might've happened automatically. As a result, data was never initialized and was null.
Thank you!