I am currently working through the FMOD Survival Shooter Tutorial. I recently rewritten the code into the enemy_health script which would call the Hellphant hurt event when the enemy was hurt. However, when I play the game, the guns bullet pass through the enemy characters and I am given the following error message:
" NullReferenceException: Object reference not set to an instance of an object
CompleteProject.EnemyHealth.TakeDamage (Int32 amount, Vector3 hitPoint) (at Assets/_Complete-Game/Scripts/Enemy/EnemyHealth.cs:61)"
“CompleteProject.PlayerShooting.Shoot () (at Assets/_Complete-Game/Scripts/Player/PlayerShooting.cs:111)”
“CompleteProject.PlayerShooting.Update () (at Assets/_Complete-Game/Scripts/Player/PlayerShooting.cs:49)”
How would I go about fixing this ?. I am also using a mac if there are any potential inconsistencies.
Thanks
NullReferenceException means that you are trying to perform an action on an object that has not been assigned/initialized. Unity does a good job of telling you exactly where these issues come from in the console window.
Eg.
Assets/_Complete-Game/Scripts/Enemy/EnemyHealth.cs:61 refers to Line 61 of the EnemyHealth.cs file. You can either attach a debugger as you run the project or even use print logs to see what variables have been assigned at that time.
Hey there,
I know I’m late to the party, but the problem is still there. There is something wrong with “eventEmitterRef.Play()” implementation. If I put it there, I’ll get the same NullReferenceException every time a bullet passes through an enemy (not HITTING the enemy!), and there is no damage done (Same as OP). As I’m not a professional Unity programmer, I also don’t know what to look for in debugging.
This is the exception I’m getting:
NullReferenceException: Object reference not set to an instance of an object
CompleteProject.EnemyHealth.TakeDamage (System.Int32 amount, UnityEngine.Vector3 hitPoint) (at Assets/_CompletedAssets/Scripts/Enemy/EnemyHealth.cs:62)
CompleteProject.PlayerShooting.Shoot () (at Assets/_CompletedAssets/Scripts/Player/PlayerShooting.cs:111)
CompleteProject.PlayerShooting.Update () (at Assets/_CompletedAssets/Scripts/Player/PlayerShooting.cs:49)
The “EnemyHealth.cs.62” is exactly where the “eventEmitterRef.Play();” is sitting. If I comment this line, everything goes back to normal.
I’m using FMOD 2.03.13 and Unity 2022.3.22f1 on Windows 11.