Audio won't play sound

Hey sorry guys, this may sound simple but I just don’t understand why the audio clip not playing. I did…

source.PlayOneShot (sound);

source.clip = sound;

source.play();

but none ever worked. The message is saying “Object reference not set to an instance of an object”
but what does it mean, I did throw in the audio sound into the public audiosource slot.

public AudioClip sound;
private AudioSource source;

void awake()
{
	AudioSource source = GetComponent<AudioSource>();

void Update()
	target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
	if ((grounded || !doubleJump) && Input.GetMouseButtonDown (0)) {
		if (target.x <= transform.position.x) {
             source.PlayOneShot (sound, 1f);

where did i go wrong?
is it cause of the audiosource file. the sound I think it’s only 0.30 sec long. it’s sounded like bloop.

`

I asked a question, and didn't get an answer.... not really helping...

2 Answers

2

“Object reference not set to an instance of an object” means you are trying to use something that is null (i.e. null reference exception).

First, make sure you capitalize “Awake()” or it will not be called, and then you will not be retrieving your audio source, so it will remain null.

Then, make sure you close the bracket for the Awake method so it compiles properly, and add brackets to the update method as well… I’m assuming this was done in the original code or it wouldn’t have been able to run at all.

Next, make sure you actually have an AudioSource component on the object, otherwise when Awake() gets called it won’t find anything.

If you still get the null reference exception after doing these things, Post the line it refers to in the error and the surrounding code, as it may not necessarily be related, or there may be something else wrong.

thank you! i'll try

NullReferenceException: Object reference not set to an instance of an object Diving.Update () (at Assets/Script/Diving.cs:67) it also says that source is assigned but its value is never used but i thought i am using it with source.PlayOneShot (sound, 1F); also Assets/Script/Diving.cs(34,29): warning CS0649: Field Diving.source' is never assigned to, and will always have its default value null'

Ahh thanks a lot, I fixed it. It was the awake that causing the problem, i had to go back and retry everything and the source.PlayOneShot works now. thanks so much

I agree with @Dave-Carlile as explained in the comment, you're struggling with the syntax in many ways. You're missing at least one semi colon, brackets are in the wrong place too. Its best to understand what you're writing rather than having it work because someone wrote it for you.

Try this:

1.) Create a GameObject on your Hierarchy.
2.) Click ‘Add Component’ then add “Audio Source”
3.) Drag the sound you want to the ‘Audio Clip’ part. (Also, dont forget to tick off the “Play on Wake” box)
4.) Create another GameObject where you will attach your script. From there, you will be asked to put the source of your ‘source’. Drag the first GameObject you created to the slot for ‘source’