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
1
Question by Saiura · Jun 12, 2014 at 04:28 PM · audiotriggeraudiosourceaudioclipaudioplay

Problem with playing sound on Trigger-Enter

I am trying to play a short sound when the player enters a trigger/walks into a coin-object. I imported the sound I want, and tried different things, but nothing worked so far.

I tried making a javascript

 var soundFile:AudioClip;
  
 function OnTriggerEnter(trigger:Collider) {
      if(trigger.collider.tag=="Player") {
         audio.clip = soundFile;
         audio.Play();
      }
 }

added the sound in the inspector, though it doesn't play at all.

I tried adding it to my C#-script I allready have for the coin and created an audio-source on the object, but it doesn't play either:

 [RequireComponent(typeof(AudioSource))]
 public class Coin : MonoBehaviour {
 
     private AudioClip gong;
 
     void OnTriggerEnter(Collider c) 
     {
     
         if (c.gameObject.tag == "Player") 
         {
             audio.PlayOneShot(gong, 1.0f);
             //audio.Play();
             Destroy (gameObject);
         }
 
     }
 }

I tried both audio.PlayOneShot, as well as audio.Play (not sure how many others there are), no sound plays at all. Is there something I need to check in the Audio-Source? I unchecked "Play On Awake" since I want the sound only when entering the Trigger. With Play on Awake, it played when loading the Scene at the beginning at least.

Comment
Add comment · Show 2
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 tanoshimi · Jun 12, 2014 at 05:13 PM 0
Share

Is this a 2D sound? If it's a 3D sound, is the AudioListener close enough to the source?

avatar image Saiura · Jun 13, 2014 at 08:18 AM 0
Share

it's a 3D Sound and the player, which I think should be the AudioListener, is exactly on the source at the time of collision... By now I have advanced with the problem and started a new question since it's a different problem: new question

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by legion_44 · Jun 12, 2014 at 05:54 PM

Are You calling Destroy on object with AudioSource that's playing the sound?

 audio.PlayOneShot(gong, 1.0f); //Here audio starts being player
 Destroy(gameObject); //And here ends, possibly even in the same frame

If You wan't to destroy this gameobject and not need settings audio effects in inspector try

 AudioClip clip;
 
 void OnTriggerEnter(Collider c)
 {
  AudioSource.PlayClipAtPoint(clip, gameObject.position);
  Destroy(gameObject);
 }
Comment
Add comment · Show 3 · 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 Saiura · Jun 12, 2014 at 06:41 PM 0
Share

That for some reason didn't seem to work... I had to change the gameObject.position to gameObject.transform.position for the code to accept it... though now, I get a lot of "NullReferenceExceptions", and when I walk into the object, it doesn't destroy, just cause some lag and adds a gameObject called "One Shot Audio" to the scene every time.

avatar image Saiura · Jun 13, 2014 at 08:19 AM 0
Share

I figured out more now, as I added as comment above, I advanced to a new problem with it. That can be found here

avatar image legion_44 · Jun 13, 2014 at 10:45 AM 0
Share

@Saiura Have You attached AudioClip in the inspector?

avatar image
1

Answer by GuntranHoet · Aug 16, 2015 at 09:38 AM

For your C# script:

  [RequireComponent(typeof(AudioSource))]
  public class Coin : MonoBehaviour {
  
      private AudioSource Source;
      public AudioClip gong;
 
 void Start()
 {
 Source = GetComponent<AudioSource>();
 }
  
      void OnTriggerEnter(Collider c) 
      {
      
          if (c.gameObject.tag == "Player") 
          {
              //audio.PlayOneShot(gong, 1.0f);
              //audio.Play();
 
              Source.Clip = gong;
              Source.Play();
 
              Destroy (gameObject);
          }
  
      }
  }
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 watercolorheartdev · Sep 27, 2017 at 02:25 AM 0
Share

Doesn't work, Source.Clip returns this: 'AudioSource' does not contain a definition for "clip' and no extension method 'Clip' accepting a first argument of type 'AudioSource' could be found.

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

25 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

Related Questions

2nd Audio Clip Not Playing 0 Answers

How Do I Get This To Play Sound? 0 Answers

OnTriggerExit stop audio 1 Answer

How to make audio not playing repeatedly? 1 Answer

How do I play audio source on Maximize on Play / Audio Source wont play on Maximize 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