Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by csgeorge · Jan 14, 2016 at 03:17 AM · c#erroraudioaudiosourceaudioclip

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges