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
0
Question by ROOKIE · Sep 21, 2011 at 02:28 AM · audioontriggerenteroncollisionenter

Play Audio on Collision or Trigger Enter

I have this code attached to my Object and I just want sound of this Object to play when it comes in collision with the Player.

For some reason the sound plays even when Player comes in contact with Enemy but neither one of those two have this audio file attached to it.

I also tried void OnCollisionEnter(Collision collision) but it didn’t help.

What am I doing wrong?

Thank you for any help.


 void OnTriggerEnter(Collider gameObject)
 {
     if (gameObject.tag == "player")
  {
     audio.Play();
  }
 else audio.Stop();
 }
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

4 Replies

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

Answer by aldonaletto · Sep 21, 2011 at 07:31 PM

You named the other object as gameObject, which is the name of a property - this may have confused the poor compiler's brain and made it read the property gameObject instead of the variable you've created. Anyway, it's a really bad idea to give the same name to different things, so I renamed the variable to otherObj. This code must be placed in the script attached to the same object as the audio source (NOT the player!):

void OnTriggerEnter(Collider otherObj)
{
    if (otherObj.tag == "player")
    {
        audio.Play();
    }
    else {
        audio.Stop();
    }
}
This will play the object's sound when the object tagged "player" touches the trigger, and will stop when other object enters the trigger or the sound finishes (Play only plays continuously if loop is set!).
NOTE1: the tag available in Unity is "Player", not "player" - make sure your tag is correctly spelled, or it will never work;
NOTE2: check if there's some other instance of this script attached to the player - it could produce the problem you mentioned.
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 syclamoth · Sep 21, 2011 at 09:22 AM

Problem here is that there is nothing to stop the sound from playing until something that is not a player enters your trigger! If it is a one-shot sound, you should use audio.PlayOneShot() instead of audio.Play (which instead sets the sound to play continuously)

Otherwise, you will need to keep track of which objects are in your collider (with a list of gameObjects) and have one sound associated with each gameObject which stops when that gameObject leaves - hardly the most straightforward of solutions!

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 ROOKIE · Sep 21, 2011 at 11:21 PM

I found my problem.

I left one of those objects in my scene - after I deleted it everything works (for now).

Thank you for your help though those are good answers

Unfortunately I can check only one as correct.

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 ROOKIE · Sep 21, 2011 at 03:20 PM

I tried that but for some reason it won't let me use PlayOneShot This is the error I get which I dont understand. :(

error CS1501: No overload for method PlayOneShot' takes 0' arguments

I also thought of using:

 IEnumerator Awake() 
 {
    audio.Play();
    yield return new WaitForSeconds(audio.clip.length);
 }

But I don't how to make it work with my:
`void OnTriggerEnter(Collider gameObject)`

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a singing bowl sing in VR, responding to incoming angle 0 Answers

OnTriggerEnter with exact spot of collision 2 Answers

My game after being built has some really weird glitches? 0 Answers

OnTrigger bullet with multiple colliders on enemy 1 Answer

Any Idea why my Explosion sound is NOT playing ? 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