• 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 Bman262 · Mar 23, 2016 at 01:47 PM · c#collisionaudiosource

Playing audio on collision

Hi im trying to play an audio clip when anything collides with my death zone area, i have the audio source on the death zone object and this script attached.

public class SawSoundController : MonoBehaviour { AudioSource saw;

 void Start ()
 {
     saw = GameObject.FindObjectOfType<AudioSource>();
 }
 
 void Update ()
 {

 }

 void OnCollisionEnter(Collision col)
 {
     if(col.gameObject)
     {
         saw.Play();
         Debug.Log("Nurrrr");
     }
 }

}

i've tried this a few different ways and nothings working, what am i doing wrong ?

Comment
Add comment · Show 3
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 TreyH · Mar 23, 2016 at 02:00 PM 0
Share

Couple things could be happening. Is there a rigidbody attached to either object involved in the collision? Is it set to Trigger? Do you have Is$$anonymous$$inematic enabled on either of them? Is one 2D and the other 3D? Any of these might cause the behavior.

Do they both have colliders?

avatar image Bman262 TreyH · Mar 23, 2016 at 04:40 PM 0
Share

There Both 3d, they both have colliders, and anything that will collide with deathZone will have a rigid body however the deathZone dose not, the death zone is a trigger but not kinematic, the objects colliding with the death zone are both triggered and un trigger but none kinematic however neither the triggered or the un triggered objects play the sound when colliding

avatar image Abhiroop-Tandon · Mar 23, 2016 at 02:04 PM 0
Share

u need to tell what objects are colliding with the deathzone. So you need to check the objects with their tag or name. i dont think it can work if i say if(col.gameObject) See if that works

4 Replies

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

Answer by JEFFDOG11111 · Mar 23, 2016 at 03:41 PM

 using UnityEngine;
 using System.Collections;
 
  //Add this Script Directly to The Death Zone
 public class SawSoundController : MonoBehaviour
 {
     public AudioClip saw;    // Add your Audi Clip Here;

     // This Will Configure the  AudioSource Component; 
     // MAke Sure You added AudioSouce to death Zone;
     void Start ()   
     {
         GetComponent<AudioSource> ().playOnAwake = false;
         GetComponent<AudioSource> ().clip = saw;
     }        
 
     void OnCollisionEnter ()  //Plays Sound Whenever collision detected
     {
         GetComponent<AudioSource> ().Play ();
     }
          // Make sure that deathzone has a collider, box, or mesh.. ect..,
          // Make sure to turn "off" collider trigger for your deathzone Area;
          // Make sure That anything that collides into deathzone, is rigidbody;
 }
Comment
Add comment · Show 6 · 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 JEFFDOG11111 · Mar 23, 2016 at 03:06 PM 0
Share

@Bman262 > tag

avatar image Bman262 JEFFDOG11111 · Mar 23, 2016 at 05:04 PM 0
Share

Thank you, it was the trigger on the DeathZone :D, bit of a nooby mistake ;)

avatar image EstebanFernandez JEFFDOG11111 · Jul 01, 2020 at 03:08 PM 0
Share

Do you hear the "Is Trigger" should be activated? What happens is that I want that when picking up a ring like those of Sonic the Hedgehog the sound sounds the rest to add it to the contandor I already have it done in that case tell me if it should be activated and if not, tell me if there is any other alternative please

avatar image wichmannoa · Nov 30, 2017 at 05:49 PM 0
Share

for those of you out there working on 2D line 17 should read "void OnTriggerEnter2D () {

Thanks for this JEFFDOG11111 it helped me a lot.

avatar image Clover10123 · Jan 14, 2018 at 08:37 PM 0
Share

$$anonymous$$y script writer says AudioClip can't implicitly convert into AudioSource... so is it important to use one or the other? Do they do different things, and if so, what?

avatar image Crystalkalem · May 17, 2020 at 06:57 AM 0
Share

I am a complete newb and have no idea where I'm meant to place the name of the audio file.... do I replace the word Saw? do I put the word after the ; ??? I have no idea. I've been at this for hours feeling dumber and dumber as nothing works no matter how I change the placements.

avatar image
0

Answer by KdRWaylander · Mar 23, 2016 at 03:56 PM

Hi,

col.gameObject is not a boolean, it's a GameObject type, it has nothing to do in your if statement.

If you do want ANYTHING going through your collider to trigger the sound, you can get rid of the if statement: whenever OnCollisionEnter is called, it plays the sound.

If you want to filter the gameobjects being able to trigger the sound, you'll need the if statement but with a boolean. Here's a possible condition that fits: col.gameObject.tag == "SomeString".

Now make sure that both objects (the one carrying the script and the one that has to trigger the sound) have colliders and that at least one of the two objects has a RigidBody.

Comment
Add comment · Show 2 · 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 b1naryatr0phy · Apr 20, 2017 at 08:21 AM 1
Share

"col.gameObject is not a boolean, it's a GameObject type, it has nothing to do in your if statement"

You're wrong, I'm afraid. His usage is both acceptable and quite common. In this context, the if-statement will return false if GameObject is null.

avatar image ransomink · Jan 14, 2018 at 09:05 PM 0
Share

As stated above, if ( col.gameObject ) is totally fine; It checks if the gameobject is null.


On another note, do not use

 col.gameObject.tag == "SomeString"

ins$$anonymous$$d do:

 col.gameObject.CompareTag("SomeString")
avatar image
0

Answer by Maggiethegsd · Jul 01, 2020 at 03:57 PM

First, make sure that your audio doesn't play on Start by unchecking 'Play on Awake'. Second, make sure that the collider in your audio trigger is set to Is Trigger. Other audio options such as 'loop' are your choice.

@Bman262

I'm saying here that the script is attached to the object through which the sound plays (lets say a ball, whenever the player touches it, sound plays) I like to create a reference and then in the editor, 'drag and drop' my Audio Source inside the 'yourAudio' blank

using UnityEngine; using UnityEngine.Audio; class Script : MonoBehaviour { public AudioSource yourAudio; void OnTriggerEnter(Collider player)

{ if (player.transform.CompareTag("Player") { yourAudio.Play(); } } } }
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 Zeesy · Jul 29, 2020 at 08:35 PM

Very simple tutorial here, works in v 2020.1: https://asyncaudio.com/blogs/tutorials/how-to-create-a-simple-audio-impact-collision-trigger-with-unity

The code they provide is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ImpactTrigger : MonoBehaviour
 {
     AudioSource source;
 
     void Start()
     {
         source = GetComponent<AudioSource>();
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         source.Play();
     }
 
 }


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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Sound plays right at the beginning 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to call OnTriggerEnter once 5 Answers

2D box colliders not touching but are colliding, how to fix? 0 Answers


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