Null Ref Exception - RPG.Combat.Fighter.Hit

Hi, I have an occasional glitch which doesn’t stop the game working but I’m getting a null reference exception in relation to the Hit method triggered by the attack animation. It seems to happen when I attack a guard but then run away. Even stranger, it only happens on my newer faster MacBook but not my 10 year old Mac mini. Is this something to do with the speed of the code execution on the faster machine.

The console is flagging up this line of code in the Fighter script.

target.TakeDamage(weaponDamage);

Thanks Daz.

This is caused by the way the animator handles transitions between states… rather than immediately switching to the next state’s animation, it blends the animations together, so it looks much less jarring. Unfortunately, this also means that any animation events that are in the 1st state can also fire off.

The solution is simple: Add this to the beginning of Hit()

if(target==null) return;

This will abort the rest of the routine, preventing a null reference error.

Brian, many thanks for your swift reply. Your solution makes sense to me and I will apply the code later. Did I miss Sam or Rick fixing this issue earlier, I’m sure it must have come up before?

Thanks again.

It was added in the lesson titled Bugs? What Bugs?

Hi Brian, I watched the bugs video, somehow I missed that one. Thanks again for your help.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms