Unity Games By Tutorials Ch7 sound issue

In Ch7, I have bene following along and have done everything up to the sound manager the bobblemarine’s audio source and the alien directly calling the sound manager to play a sound.

My problem is that I get a null error when an alien is killed. So being confused I added a print(); function before the clip is to be played in the soundmanager grabbing clip.name to see if it came out null. it didn’t it correctly called the alien death sound from the sound manager.

however, the following call soundEffectAudio.PlayOneShot(clip); gives a null error. What could be causing my null reference exception?

Also adding a audio source to the alien prevents this error from occurring but, the sound doesn’t play.

Do you have an AudioSource component attached to the SoundManager game object?

Yes I do but that audio source object is used for playing the background music.

After re-reading the section carefully and taking out my debug print() line I saw my issue.

I didn’t have a second audio source for it to set to the soundEffect object and printing out a null clip name causes a null reference error.

Exactly as I suspected. Making a call to an AudioSource that didn’t exist. Glad you got it put in so that you can keep moving forward with the project :-]