I have assigned it, but I get an error of Unassigned Reference Exception!

I have a GameObject with a script and an Audio Source.
The Audio Source is assigned to the variable of the script.
While testing the game in Play mode though, the sound doesn’t work, and I get an Unassigned Reference Exception Error!


I tried assigning the audioPlayer variable both by dragging and dropping the Audio Source component in the Inspector, and using the GetComponent() method. None worked.

Now why is this happening ?

Edit: Here is some more info:

I used to initiate the Audio Source variable with a call to GetComponent() in the Start() function of the script, and the call to the source’s Play() function would take play whenever the character would press the attack button.

And although in the beginning it was working, something happened and then it wouldn’t.
But I tried this: Using the GetComponent() in the function that is called when the player hits the attack button. This way the sound plays! Although its play is delayed because it has to get the component the moment it needs to play the sound.

I tried


void SomeClass : MonoBehaviour

AudioClip attackSound; 

void Start()

attackSound = GetComponent<AudioSource>().clip;


I retrieve the actual clip in this situation so you can load it from Resources or prefabs or change it dynamically right from the go! Be sure to remove the script from the GameObject and put it back on or this WON’T work for some dumb reason.