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