• 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 Zitoox · Jul 06, 2016 at 07:45 PM · collisionscripting beginnersound

Play sound on collision

It's a very basic script, but i didn't found anything like this in the web, so, i will ask here, but as i said, it's very simple i am just too bad to know what to do xD (seriously, i am new to scripting).

I made a simple script that play a sound everytime ANYTHING touches the collider of my object.

 #pragma strict
 
 var bipsoundClip:AudioClip;
 
 function OnTriggerEnter(o:Collider){
 
 
     GetComponent.<AudioSource>().PlayOneShot(bipsoundClip);
 }

Let's say i have a cube named "Atlas", (it's just an example) i want to play my sound "bipsound" everytime a SPECIFIC object collides with my cube Atlas. How can i do this? PS: Only a SPECIFIC object/prefab, not the other ones. And i want it to loop.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by EDevJogos · Jul 08, 2016 at 08:21 PM

AudioSource has the atribute Loop just set it to true.

There are many ways to check which object is colliding, one is checking the name atribute of the collider and make a if statement to compare with the ones you want, like:

 if(other.gameObject.name == "MyObject")
 {
        //Do something
 }

You can also do it with tags or even with a specific atribute from a specific componente, like:

 //You should check if the componente is null in case of many objects with different components colliding.
 if(other.transform.GetComponent<ScriptName>().myVariable == value)
 {
    //Do something
 }
 

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
1

Answer by BloodBTF · Jul 09, 2016 at 02:39 PM

You can create a custom tag for that specific object, assign that tag to the object you want, then in the script you can do something like this.

 function OnTriggerEnter(other : Collider)
 {
 if (other.transform.tag == "your tag")
 {
 GetComponent.<AudioSource>().PlayOneShot(yoursound);
 }
 }`
 

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 Petch12 · Jul 07, 2016 at 05:53 PM

Try attaching this script (and the audio source) to the specific object (that way when that object collides with something else - the cube - it will play the sound).

This for the looping: http://answers.unity3d.com/questions/541999/how-to-play-loop-an-audio-clip-on-button-hold-down-1.html . (Sorry don't have a java version).

Alternatively try clicking loop audio on audio source.

Hope this helps.

Comment
Add comment · Show 4 · 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 Zitoox · Jul 08, 2016 at 07:52 PM 0
Share

This doesn't makes sense! You're telling me to do exactly the opposite of what should be done, this is ridiculous ...

avatar image Petch12 Zitoox · Jul 09, 2016 at 11:35 AM 0
Share

I don't quite know how you can claim this is wrong (because it works!). Initially you said you made a script to play a sound when any object collides with the cube. You can either (as @Search said) check the object colliding with the cube then if the object is the specific object execute the code to play the sound (this is the most efficient way), or (on the flip side) you could change it so that every time the cube touches the specific object it plays the sound. It's exactly the same both ways (It's just a different way of thinking about it -if one way doesn't succeed try another way!) just one is more efficient. I'm not sure how you didn't understand to be honest the logic behind it is simple...

avatar image Zitoox Petch12 · Jul 09, 2016 at 09:05 PM 0
Share

You said to attach my script to my specific object. Why this doesn't makes sense? Because i wanted $$anonymous$$Y CUBE to play a alert sound when a specific object collide with it. If i attach $$anonymous$$Y SCRIPT to THE SPECIFIC OBJECT, it will make a sound, oh wait, it will not BECAUSE IT WILL NOT COLLIDE WITH $$anonymous$$Y CUBE AND IT WILL PLAY THE SOUND EVERYTI$$anonymous$$E IT COLLIDES WITH ANYTHING '-'

Show more comments

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

65 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

Related Questions

Dice roll collision sound 1 Answer

Play sound on collision 1 Answer

How to get sound to play on barrel Explosion 1 Answer

Multiple sound for different occasion 0 Answers

Making a sound using collisions 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