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 Patton7 · Aug 14, 2018 at 09:33 PM · collision2d game

Audio Source Play On Collision 2D?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CoinCollect : MonoBehaviour {

 public GameObject coin;
 public AudioSource coinc;





 void OnTriggerEnter2D(Collider2D coll)
 {
     if (coll.gameObject.tag == "PlayerTag")
     {

         coinc.Play();

         Destroy(coin);
         
     }
 }
 }

Hello! I looked everywhere for this and I couldn't find it. I'm trying to play an Audio Source on collision (2D) but the audio won't play. I know the function works, because the Game Object does get destroyed. However, the sound does not play. Can anyone help?

Thanks.

Comment
Add comment · Show 6
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 Hellium · Aug 14, 2018 at 09:49 PM 0
Share
  1. Are you sure you have provided an AudioClip?

  2. Are you sure you have turned on the sound on your computer?

  3. Are you sure the volume is high enough?

  4. Can you tell if the sound plays when you check playOnAwake on the AudioSource component?

avatar image Patton7 Hellium · Aug 18, 2018 at 04:10 AM 0
Share
  1. Yes.

  2. Yes.

  3. Yes.

  4. Yes. In fact, I still did do coinc.Play(); but I changed the condition and worked. it's just the collision condition that refuses to work.

avatar image bennett_apps · Aug 15, 2018 at 03:23 AM 0
Share

The code is fine! You just need to quadruple check your audiosource component, the name of the tag for your player, & make sure you dropped an audiosource component on the coinc variable.

avatar image Patton7 bennett_apps · Aug 18, 2018 at 04:21 AM 0
Share

I did do all those things and the tag is fine. The thing is, it said the audio did play. Before, I accidentally destroyed by game object before I played the audio. I got the following error. "Disabled Audio Source Cannot be Played". This meant it was getting to the code, but it sill wasn't playing. I switched the sequence of the two. Then, I did coinc.Play(); but had a different condition for when it should play, and it worked. I think the issue is the fact that the audio is playing on 2D collision, but I'm not sure.

avatar image RShields · Aug 15, 2018 at 03:47 AM 0
Share

OnTriggerEnter2D requires at least one of the objects have a RigidBody2D attached and I think it can't be Static. In addition, if your AudioSource is attached to the object you're destroying, it'll destroy the AudioSource before it plays.

avatar image Patton7 RShields · Aug 18, 2018 at 04:16 AM 0
Share

For your first thing, my player has a rigidbody2d. If I set it to static, nothing in my scene moves, and it kinda screws with the rigidbody.

And I did previously have that issue, but I just made the audio play first, then have the object subsequently be destroyed. The error for that stopped popping up.

3 Replies

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

Answer by rainChu · Aug 18, 2018 at 06:18 AM

There's a lot that might be going wrong, but one thing to check is the position of your audio source. By default, an AudioSource plays sound to your AudioListener based on its 3D position. If it's too far away, you won't hear it, even if it plays at max volume. Is your AudioListener attached to the camera or view (where the players ears will be), at a reasonable distance from the AudioSource?


Is your AudioSource attached to the coin? If so, as soon as that coin is destroyed, the AudioSource will be destroyed too. It only plays while it's alive. To solve this issue, I like to have a special prefab just for my audio effects, like this:

 // Have a prefab with an AudioSource set to playOnAwake.
 // You should also add a script to this Prefab which calls Destroy() on its own gameObject when the SFX has stopped playing. You can wait until !isPlaying in its Update(), or you can start a coroutine in its Awake() if you want to be efficient.
 
  public GameObject soundEffectPrefab;
 
  void OnTriggerEnter2D(Collider2D coll)
  {
      if (coll.gameObject.tag == "PlayerTag")
      {
          var effect = Instantiate( soundEffectPrefab );
          effect.transform.position = transform.position;
 
          Destroy(coin);         
      }
  }

You can use my own sound effect script to get started with an effect prefab.


soundeffect.zip (1.1 kB)
Comment
Add comment · Show 3 · 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 Patton7 · Aug 18, 2018 at 04:25 PM 0
Share

I did what you said and still does not work. Additionally, I don't think that the position is the problem. I changed the condition in which coinc.Play(); should be executed, (I put it in the start method) and it worked. The collision condition makes it not play, however. But thanks for trying.

avatar image rainChu Patton7 · Aug 18, 2018 at 10:50 PM 0
Share

I'm still pretty sure that Destroy() is the actual problem. Check the second part of the answer.

avatar image Patton7 · Aug 31, 2018 at 02:00 AM 0
Share

WOW! Thanks this helped and it worked!

avatar image
-1

Answer by Usama_S · Aug 18, 2018 at 05:44 AM

Did you assign a clip to audio source. if not then you can use this,

public GameObject coin; public AudioSource coinc; public AudioClip coinClip; void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject.tag == "PlayerTag") { coinc.clip = coinClip; coinc.Play(); Destroy(coin); } }

Comment
Add comment · Show 1 · 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 Patton7 · Aug 18, 2018 at 05:57 AM 0
Share

I did assign the clip and with your code it sill does not work.

avatar image
0

Answer by bennett_apps · Aug 19, 2018 at 02:38 AM

Wait...is the audiosource on the coin? because if it is destroying the coin immediately after will definitely not work!!!!!!!

Comment
Add comment · Show 3 · 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 bennett_apps · Aug 19, 2018 at 02:38 AM 0
Share

If so, you'll have to use AudioSource.PlayClipAtPoint

avatar image Patton7 · Aug 30, 2018 at 04:06 AM 0
Share

I tried doing what you said, but my vector 2 for PlayClipAtPoint is a float and vector 2s can only take doubles.

avatar image rainChu Patton7 · Aug 30, 2018 at 04:14 AM 0
Share

That is incorrect. Your Vector2 is not a float, it is a Vector2. It's its own thing. It's a struct. They take floats. See the manual on PlayClipAtPoint for how to use it. It requires a position, followed by a volume. The script I provided in my answer also does the same thing. It takes longer to implement, but it has more features.

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

159 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 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 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

Problems with 2D Collision 3 Answers

create game 2d linking stars 0 Answers

Collider detection in 2D 1 Answer

Problem with collision with two colliders simultaneously in Unity 2D 1 Answer

Why my ball acts if there is a hidden collider? 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