Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Azi · Mar 28, 2013 at 07:00 PM · audiosoundaudiosource

Getting an AudioSource to play?

I used to have a script that would get my player prefab to play a 2D sound whenever a specific object was instantiated. I recently updated to Unity4.1 (previous version was 3.5.x) and now my script doesn't work, and I constantly get an error saying

Can not play a disabled audio source

UnityEngine.AudioSource:PlayOneShot(AudioClip) I did do some research and it's apparently because it won't play a sound from a prefab but I can't seem to find another way of doing this. This is my script on the Instantiated object. var explosionSound : AudioClip; var audioSource : AudioSource;
function Start(){ audio.enabled = true; audioSource.PlayOneShot(explosionSound); }
Where audioSource is my player prefab, and I have 2 slightly modified versions of that prefab in my scene (player 1 and player 2) What is the best possible way to get my sound played? Any help would be greatly appreciated.
Comment
Add comment · Show 1
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 hexagonius · Mar 29, 2013 at 07:42 PM 0
Share

$$anonymous$$y first quess is either the gameobject this script is attached to does not have an AudioSource or it is not enabled through the checkbox in the Inspector

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by psycocrusher · Mar 29, 2013 at 09:39 PM

You dont need a script for that, drag your audio file on the audio source, and check "Play on Awake" and uncheck "Loop".

The audio should only play once when you instantiate it.

Comment
Add comment · Show 5 · 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 Azi · Mar 30, 2013 at 06:15 PM 0
Share

That would make sense, however the gameobject destroys itself after a second, and the audio clip lasts 2-3 seconds. so I end up hearing only part of the file.

avatar image fafase · Mar 30, 2013 at 06:22 PM 0
Share

That is why my previous answer (that I removed since it was not what you wanted but I can put it back) was using AudioSource.PlayClipAtPoint which actually creates a new object of type AudioSource that will live for the duration of the audio clip. It will actually show in the hierarchy as an independant object meaning that destroying the object that created it will not affect its life. But you said this would not fix your issue.

avatar image Azi · Mar 30, 2013 at 06:57 PM 0
Share

I don't remember seeing an answer about PlayClipAtPoint, the only other answer I saw was someone suggesting audio.Play()

How do you implement PlayClipAtPoint?

avatar image fafase · Mar 30, 2013 at 07:01 PM 0
Share

Oops I meant PlayOneShot. But you can try PlayClipAtPoint:

 AudioSource.PlayClipAtPoint(sound, transform.position);
avatar image Azi · Mar 30, 2013 at 07:19 PM 0
Share

that seemed to work. thank you very much

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

12 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

Related Questions

How to start a sound not from it's start ? 2 Answers

footstep sound 1 Answer

Sound Clip isn't playing when triggered. 1 Answer

One AudioSource on player or one assigned to every collectable (hundreds)? 1 Answer

Playing a sound clip from another object 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