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
3
Question by mahdi68 · Nov 20, 2011 at 03:39 PM · audiosoundcoincollections

play sound on collecting pickUp Item

Hi i want to play sound effect when coin collecting(pickup item) in my game , i use this code :

  var collectSound:AudioClip;
 
  function OnTriggerEnter (other:Collider) {
 
   if(other.gameObject.tag=="Player"){
      audio.PlayOneShot(collectSound);
      Destroy(this.gameObject.collider);
      Destroy(this.gameObject);
    }

but this part not working :

 audio.PlayOneShot(collectSound);

can any one help me ? thanks

}

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
7
Best Answer

Answer by aldonaletto · Nov 20, 2011 at 03:57 PM

You're destroying the object before the sound finished playing. You could use PlayClipAtPoint:

function OnTriggerEnter (other:Collider) {
  if(other.gameObject.tag=="Player"){
     AudioSource.PlayClipAtPoint(collectSound, transform.position);
     Destroy(gameObject); // this destroys the collider as well
  }
}
PlayClipAtPoint creates a new temporary AudioSource instance at the point informed, which will be destroyed when the sound finishes.
Comment
Add comment · Show 8 · 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 FLASHDENMARK · Nov 20, 2011 at 04:09 PM 0
Share

You read my $$anonymous$$d, or maybe I read yours. Either way...

avatar image mahdi68 · Nov 20, 2011 at 04:23 PM 0
Share

Thanks it work fine

avatar image Zero_Zagarth · Dec 06, 2012 at 07:15 PM 0
Share

Thank you men! That was very until for me hehe

avatar image AwsomeRod · Dec 08, 2012 at 01:27 AM 0
Share

I DONT GET IT

avatar image AwsomeRod · Dec 08, 2012 at 01:27 AM 0
Share

What PlayClipoint

Show more comments
avatar image
1

Answer by henry96 · Nov 20, 2011 at 03:46 PM

I think that line of code, which you have trouble with, reqiure audio source. So simply select the object, which the sound will play from, then go to component -> audio -> audio source. Hope this helps.

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 himanshu619 · Dec 07, 2012 at 04:21 PM

Full working Code:

 @script RequireComponent(AudioSource)
 
 public var clip : AudioClip;
 
 function OnTriggerEnter(hit : Collider){        
         audio.PlayClipAtPoint(clip, Vector3 (5, 1, 2)); 
         Destroy(gameObject);    
 }

Now place this javascript on to the gameobject (i.e.coin) and audio source, After it in inspector window under the above script select the audio clip.

Comment
Add comment · Show 2 · 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 AwsomeRod · Dec 08, 2012 at 01:27 AM 0
Share

Hey it doesnt work... i added the script to the object to collect and i collected it and no audio sounded... Y

avatar image himanshu619 · Dec 11, 2012 at 04:06 AM 0
Share

This is because you haven't added any sound in your script (under inspector window). Add any sound in the script (from inspector window). $$anonymous$$ark this answer as right and do get back to me if it still not work for 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

10 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

Related Questions

Is there a way to create a random Audiosource loop? 2 Answers

Gun Audio reverbs off of Terrian- sounds horrible- What can I do? 0 Answers

Play sound on animation event? 3 Answers

error CS0029: Cannot implicitly convert type void' to UnityEngine.AudioSource' 1 Answer

Realistic Sound Effect 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