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
0
Question by LittleJ · May 28, 2013 at 12:32 PM · triggersound

Playing sound on collide with a trigger.

I created an empty object and named it "soundtrigger1" Now i used the script which i found on another question.

function OnTriggerEnter(otherObj: Collider){ if (otherObj.tag == "Graphics"){ audio.Play(); } else { audio.Stop(); } }

The "Graphics" is the actual player on the First Person controller script which comes with Unity standard assets. I have attached a box collider to the empty object and made it a trigger. I attached a sound to the object aswell so when the "Graphics" collides with it, it will play a sound. This doesnt work.. Is there another way to make sound play on collision? i have been researching this for a long time and couldn't find the answer i was looking for. Thanks for your time.

Comment
Add comment · Show 1
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 ragnaros100 · May 28, 2013 at 12:45 PM 0
Share

does the Graphics gameObject have a collider? and does the player object have a collider too?

otherwise you could use OnCollitionEnter() ins$$anonymous$$d. I would still not recomend using triggers and physics to detect stuff. Unity and physX doesnt allways speak that well to eachother

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bobin115 · Sep 08, 2014 at 10:28 AM

try this simple script

 var soundFile:AudioClip;
  
 function OnTriggerEnter(trigger:Collider) {
      if(trigger.collider.tag=="Player") {
         audio.clip = soundFile;
         audio.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 TonyLi · May 28, 2013 at 03:43 PM

Things to check:

  • In Edit > Project Settings > Physics, make sure that the layers assigned to your first person controller and soundtrigger1 are set to register collisions (checkbox is ticked).

  • Double-check that the first person controller object has a collider, and that this object's tag is really "Graphics" (with exact capitalization).

  • You probably don't need "audio.Stop()". This might be causing a problem if another collider (perhaps on a child object of the first person controller) is also entering the trigger.

  • Add some debugging lines to see what's actually happening:

           function OnTriggerEnter(otherObj: Collider) {
                 Debug.Log(otherObj.name + " entered trigger");
                 if (otherObj.tag == "Graphics") {
                     Debug.Log("Tag is Graphics; playing audio");
                     audio.Play();
                 }
             }
    
    
  • If you get " entered trigger" you know that the trigger happened.

  • If you do not get "Tag is Graphics; playing audio", then whatever entered the trigger doesn't match your condition for playing audio.

  • If you do get "Tag is Graphics; playing audio" but no audio plays, then there's something wrong with the AudioSource component on soundtrigger1 (or your speakers are turned off) :-)

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Need help making an audio trigger. 2 Answers

How to make the trigger work only once. (SOUND) 1 Answer

I want my trigger sound only to play once! 0 Answers

How would I make a laser beam sound? 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