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)

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;

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:


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!

@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!