You’ve probably already fixed it by now, on your own, but here’s what it is and what I did to fix it in case anyone else needed it.
It’s throwing an exception anytime the raycast hits anything that’s not an Enemy GameObject or on the Walkable layer. Looking for an enemy GameObject in the ‘if’ statement or the variable, seems to be throwing it off.
My game uses fixed camera angles that sometimes show the sky, or a wide background. So I got the error constantly, and it was driving me insane. That’s why it usually only happened to you when your cursor went over the editor, and is now very sporadic. Your camera is always facing the ground. It’s just those rare moments when your cursor ray hits something that isn’t an enemy or something on Walkable.
I switched it to look for the enemy layer instead, and still returned the Enemy GameObject to the delegate. Everything still seems to work. A pretty simple change. Let me know if there’s a reason why we wouldn’t want to do it this way.
Code snippet:
https://pastebin.com/xu6tar3F