- Home /
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.
Are you sure you have provided an AudioClip?
Are you sure you have turned on the sound on your computer?
Are you sure the volume is high enough?
Can you tell if the sound plays when you check
playOnAwake
on theAudioSource
component?
Yes.
Yes.
Yes.
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.
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.
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.
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.
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.
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.
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.
I'm still pretty sure that Destroy() is the actual problem. Check the second part of the answer.
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); } }
I did assign the clip and with your code it sill does not work.
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!!!!!!!
If so, you'll have to use AudioSource.PlayClipAtPoint
I tried doing what you said, but my vector 2 for PlayClipAtPoint is a float and vector 2s can only take doubles.
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
Follow this Question
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