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.
Hi,
Thank you for bringing this to our attention.
Has the eventEmitterRef variable been assigned an Unity Integration | Game Components - Studio Event Emitter either in the editor:
or via code with something like this:
public FMODUnity.StudioEventEmitter eventEmitterRef = null;
void Start()
{
eventEmitterRef = GetComponent<FMODUnity.StudioEventEmitter>();
}
Which video did you get up to before hitting this issue?
Hello again, and thanks for the reply.
I actually got frustrated and moved on to another tutorial series, hence the late answer.
I was on this video (10:12 to 11:37) and did things same as in the video. So I have used the second method (assigning the eventEmitterRef variable via code).
Now that you’ve mentioned another approach, I tried that, too, but got the same error on the same line again.
Is there any additional information I can provide to help you replicate or tackle the error?
Thanks for the info.
From the video I think we need to make sure that for the prefab Assets/_CompletedAssets/Prefabs/Hellephant.prefab it has the event emitter added to it
So that our script can pick it up:
void Awake ()
{
// Setting up the references.
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
// Setting the current health when the enemy first spawns.
currentHealth = startingHealth;
// Grab emitter from the game object
eventEmitterRef = GetComponent<FMODUnity.StudioEventEmitter>();
}
Could I please grab a screenshot of your Hellephant prefab?
Sure, here you are
I’m pretty sure everything is in place and I haven’t missed anything. The error I’m facing only occurs when eventEmitterRef.Play()is present. When I comment it everything goes back to normal. It looks like this method somehow disrupts the code related to enemy collider recognition by player’s bullets.
The NullReferenceException provided in my first post mentions other methods such as CompleteProject.PlayerShooting.Shoot () and CompleteProject.PlayerShooting.Update (). It seems that eventEmitterRef.Play()is just the starting point and the main issue occurs somewhere else.
Thanks for confirming that, would it be possible to upload your full EnemyHealth.cs file as a txt files? I can add it to my SurvivalShooter project to see if I can preproduce the issue.