enemyDeathSFX not playing

Hi,
I am using unity 2018.2.6 for this project.
I made the changes as mentioned in the lecture but the sound is not playing. I am getting the below error in console.

" NullReferenceException: Object reference not set to an instance of an object
EnemyDamage.KillEnemy () (at Assets/Scripts/EnemyDamage.cs:42)
EnemyDamage.OnParticleCollision (UnityEngine.GameObject other) (at Assets/Scripts/EnemyDamage.cs:26) "

The line 42 in my script is

AudioSource.PlayClipAtPoint(enemyDeathSFX, Camera.main.transform.position);

This error came only after I replaced “transform.position” with “Camera.main.transform.position”.

A clip of game play after making all the changes for enemyDeathSFX.
https://drive.google.com/file/d/1HzUVNPY2BxdVd13ckMo38A1IeBMbM6Tb/view?usp=sharing

Hi Dhruv,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

As you can see in the video clip link I have shared already, all the exposed fields in EnemyDamage script on Enemy prefab are filled.
But still the error is coming.

Also, the line for which error is shown is the PlayClipAtPoint line I have mentioned before. If in that line I replace “Camera.main.transform.position” with just “transform.position”, I don’t get any errors.

But as per the lecture, to fix the audio issue, we need it to be “Camera.main.transform.position”.

So, why using “Camera.main.transform.position” is causing this error?

Does the camera have got the MainCamera tag in its Inspector?

The main camera was untagged.
Tagged it as Main Camera and it fixed the issue.
Thanks!

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

Privacy & Terms