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.

`

“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.

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’