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 jiomancy · Apr 08, 2018 at 06:59 AM · triggeraudiosourceexitenter

Audio Trigger Enter Exit

Having an issue where I want an audio track to play when the player enters the collision area and end when the player leaves. I can't get either function to work and this is as far as I have gotten with the research I have done.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class source : MonoBehaviour {

 public AudioClip winner;
 AudioSource audioSource;

 private void OnTriggerEnter(Collider other)
 {
     if (other.CompareTag("Player"))
     {
         audioSource.PlayOneShot(winner);

     }
 }
 private void OnTriggerExit(Collider other)
 {
     if (other.CompareTag("Player"))
     {
         audioSource.Stop;

     }
 }


}

Comment
Add comment · Show 9
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 Sabre-Runner · Apr 08, 2018 at 07:06 AM 0
Share

This should work. Are you sure that both functions are playing? Are they even triggered? Did you make sure the player also has a collider?

Also, did you try just 3D sound with rolloff 0 at the distance you want it to stop?

avatar image jiomancy Sabre-Runner · Apr 08, 2018 at 07:23 AM 0
Share

I added a Debug.Log to test if the triggers worked and they don't. Also I've tried out the 3D sound, and it works but sort of does what I want to do. I have background music already, so if it could stop as I entered the 3D sound radius that would be ideal as well, but as of right now both tracks play when I enter.

avatar image Sabre-Runner jiomancy · Apr 08, 2018 at 01:36 PM 0
Share

Do all your triggering objects have a collider?

Show more comments

1 Reply

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

Answer by Chimer0s · Apr 08, 2018 at 10:41 PM

You may need to declare the winner clip for this to work as you've written it. Although if you're trying to play a looping audio source, there's no need to even go that far. You could simply use:

     private void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Player")
         {
             if (!audioSource.isPlaying)
                 audioSource.Play();
         }
     }
     
     private void OnTriggerExit(Collider other)
     {
         if (other.CompareTag("Player")
             audioSource.Stop();
     }

Comment
Add comment · Show 1 · 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 jiomancy · Apr 14, 2018 at 08:57 PM 0
Share

Awfully sorry for the late reply, but I tried this and it did end up working. Thank you!

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

96 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 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

Why isn't OnTriggerEnter/Exit working? 2 Answers

Using Tags with OnTriggerExit 3 Answers

When inside trigger, turn object on, when outside trigger, turn off 1 Answer

OnTriggerExit() help 0 Answers

How do I have the object disappear once the FPS controller looks at and then looks away. 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