I’m trying to use the PlayClipAtPoint function to play an audio clip at the position of the GameObject the script containing that code is attached to. This is the code I have:
And I am getting this error:
Assets/Scripts/EnemyHealth.cs(43,53): error CS0176: Static member `UnityEngine.AudioSource.PlayClipAtPoint(UnityEngine.AudioClip, UnityEngine.Vector3)’ cannot be accessed with an instance reference, qualify it with a type name instead
I’ve seen several threads dealing with this error, but I’m still not sure exactly what it means or how I should edit my specific code to resolve it. Can anyone help?
To fix this specific problem instead use
An AudioSource component will use the position of the Transform on the GameObject that it is attached to as its playing location. You could also assign the clip in the inspector and just call
to play the loaded clip.
PlayClipAtPoint is a static member of the AudioSource class. Without getting into too many details this means that it cannot be accessed from an instance of an AudioSource (like the component you are accessing) and only has access to the data in the parameters provided to it and any other static members of AudioSource. That’s what the error message is talking about.
The PlayClipAtPoint will create a new AudioSource instance at that point, load that clip and play the sound at its position. The other two methods will use an existing AudioSource to play a clip where ever that AudioSource happens to be.
this answer helped me with the same problem!
Although @EternalGoldenBraid’s answer is great, and he probably knows more than I do, I’ll just restate his point in another way.
PlayClipAtPoint() cannot be called off of an instance of the AudioSource class. It MUST be called from the class itself.
PlayClipAtPoint() belongs to the AudioSource class itself, not any particular instance of the class.