- Home /
PlayClipAtPoint Qualify with Type Name
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:
GetComponent<AudioSource>().PlayClipAtPoint(destroySound,transform.position);
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?
Answer by EternalGoldenBraid · Jan 14, 2016 at 03:45 AM
To fix this specific problem instead use
AudioSource.PlayClipAtPoint(destroySound, transform.position);
or
GetComponent<AudioSource>().PlayOneShot(destroySound);
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
GetComponent<AudioSource>().Play();
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.
Answer by aaronrums · Jan 08, 2021 at 10:15 PM
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.
Your answer
Follow this Question
Related Questions
How to play "audiosource" component in a prefab from script? 0 Answers
Triggering multiple audio clips to play in sequence 1 Answer
How can I play audio clips depending on the players movement 0 Answers
Audio Clip Playing every frame 2 Answers
Audio/c#/unity Can i control the duration by time ? 1 Answer