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
1
Question by Abacab · Jan 21, 2015 at 06:39 PM · c#audiocoin

Audio doesn't play when collecting a coin

Hello!

I want the game to play a sound when the player collects a coin, but the sound doesn't play. What am I doing wrong?

 public GameObject Coin;
 public AudioClip soundFile;
 
     void OnTriggerEnter ()
             {
                     if (Coin.gameObject.tag == "Coin") {
                             CoinController.coins++;
                             CoinController.coinsCollectedThisLevel++;
                             PlayerPrefs.SetInt ("Coins", CoinController.coins + 1);
                             audio.clip = soundFile;
                             audio.Play();
                             Destroy (Coin.gameObject);
                     }
             }
Comment
Add comment · Show 2
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 MakinStuffLookGood · Jan 21, 2015 at 06:44 PM 1
Share

Could be a number of things:

Is the AudioSource you're attempting to play from attached to the GameObject you're destroying? If so, you won't hear the clip because you destroy it on the same frame. Try the static AudioSource.PlayOneShot and see if that works.

Is the sound set as a 3D sound and the scene's audio listener is too far away to hear it? You may want to go to the import settings and make the coin pickup sound 2D.

Check these two things and report back.

avatar image Abacab · Jan 21, 2015 at 07:01 PM 0
Share

Changed the audio file to 2D sound and changed the script. Still doesn't work. :(

 void OnTriggerEnter ()
         {
 
                 if (Coin.gameObject.tag == "Coin") {
                         CoinController.coins++;
                         CoinController.coinsCollectedThisLevel++;
                         PlayerPrefs.SetInt ("Coins", CoinController.coins + 1);
                         audio.PlayOneShot(soundFile, 1);
                         Destroy (Coin.gameObject);
                 }
         }

2 Replies

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

Answer by Chris333 · Jan 21, 2015 at 08:33 PM

Hi, i think Broxxar already found the Problem. You play the clip and destroying the object at the same time. The audio reference returns the AudioSource of the gameObject. http://docs.unity3d.com/ScriptReference/GameObject-audio.html

Try to call destroy with the delay of the length of your audio clip.

 Destroy(Coin.gameObject,audio.clip.length);
Comment
Add comment · Show 12 · 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 Abacab · Jan 21, 2015 at 08:36 PM 0
Share

This definitely worked, thanks to you and Broxxar. However if I use Destroy(Coin.gameObject,audio.clip.length); The collection of the coin looks kind of stupid. I'll have to think of another way.

avatar image Chris333 · Jan 21, 2015 at 08:39 PM 0
Share

Just use a central AudioSource in your scene which plays the sounds. Than you dont need to delay the destroy $$anonymous$$ethode.

avatar image Superrodan · Jan 21, 2015 at 08:42 PM 0
Share

What you might want to do is have the coin point to an external AudioSource attached to the character or camera. In the code where it destroys itself it tells the AudioSource to play before it destroys itself.

Then the AudioSource is not destroyed with the coin but the coin disappears instantly.

avatar image Chris333 · Jan 21, 2015 at 09:22 PM 1
Share

Try this:

 public GameObject Coin;
     public AudioClip soundFile;
     public AudioSource $$anonymous$$C;
 
     void Start () {
         $$anonymous$$C = Camera.main.GetComponent<AudioSource>();
     }
 
     void OnTriggerEnter ()
     {
         
         if (Coin.gameObject.tag == "Coin") {
             CoinController.coins++;
             CoinController.coinsCollectedThisLevel++;
             PlayerPrefs.SetInt ("Coins", CoinController.coins + 1);
             $$anonymous$$C.PlayOneShot(soundFile);
             Destroy (Coin.gameObject);
         }
     }
avatar image miahpow Chris333 · Mar 04, 2016 at 03:13 PM 0
Share

its giving me an error for coin collector

avatar image Abacab · Jan 21, 2015 at 09:25 PM 1
Share

Bingo Chris333! Thank you all for your help.

Show more comments
avatar image
0

Answer by sniper43 · Jan 21, 2015 at 10:53 PM

Boxxar has found your problem, But here's another solution:

Replace

 audio.Play();

with

  AudioSource.PlayClipAtPoint(audio, Coin.transform.position);



This creates a seperate game object that is a sound source but has no body and self destructs after the audio clip is over.

This is better assuming the object has a collider and you don't wish for an animation to play.

When you have an animation it is better to use

 Destroy(Coin.gameObject, YourAnimationObjectHere.clip.length)

I got the idea from http://unity3d.com/learn/tutorials/projects/stealth/the-key

You shold do the stealth tutorial if you haven't already.

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

29 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[SOLVED] Issue with music and Don't Destroy on load 2 Answers

[c#]Does audio.Play() work only in Update and Start funcitons? 2 Answers

Text not setting to correct value 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