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 /
  • Help Room /
This question was closed May 10, 2016 at 05:50 PM by meat5000 for the following reason:

Duplicate Question

avatar image
0
Question by SaintTippu · May 10, 2016 at 03:46 PM · audio

Play audio on Hit

Hi, I want to play the audio of an object when my player hits it. For example, my player when hits the object, the sound of that object plays 1 time and my player collects 1 point from it. I know the point collection but the sound ain't playing when my player hits it.

Comment
Add comment · Show 3
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 fabian-mkv · May 10, 2016 at 04:41 PM 0
Share

Could you show your (relevant) code so we can see what you tried?

avatar image aballif · May 10, 2016 at 04:44 PM 0
Share

I am trying to do the same thing with my coins. When The player touches the coin, it is supposed to play the sound and then set the coin to Active(false). Is the code just not giving the audio clip enough time to finish before it becomes inactive?

avatar image meat5000 ♦ · May 10, 2016 at 05:50 PM 0
Share

This information is in the manual and has been answered many times.

2 Replies

  • Sort: 
avatar image
0

Answer by JoshDangIt · May 10, 2016 at 04:32 PM

Simplest way is to call AudioSource.PlayClipAtPoint().

http://docs.unity3d.com/ScriptReference/AudioSource.PlayClipAtPoint.html

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 Ali-hatem · May 10, 2016 at 05:14 PM

@SaintTippu you can simply attach the audio source to the player & change the clip as needed when collide with something @aballif to avoid playing desabled AudioSource when deactivating object that hold the sound :

 public AudioClip coin;
 public AudioClip wall;
 AudioSource audio;
 void Start(){
     audio = GetComponent<AudioSource>();
 }  
 void OnCollisionEnter(Collision other){
     if(other.gameObject.tag == "coin"){
          audio.clip = coin;
          audio.Play();
     }
     if(other.gameObject.tag == "wall"){
          audio.clip = wall;
          audio.Play();
     }
 }

& make sure objects have collider attached & Rigidbody so colliders work fine & if it's trigger colliders use OnTriggerEnter & if it's 2D project use OnCollisionEnter2D or OnTriggerEnter2D & lastly you can vice-versa because you can't play desabled AudioSource.

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 aballif · May 10, 2016 at 05:35 PM 0
Share

If I only have One audio clip for the object, can I leave of the Public AudioClips and leave of the audio.clip = coin?

avatar image Ali-hatem aballif · May 10, 2016 at 05:48 PM 0
Share

yes just call audio.Play();if you have attached the clip to the AudioSource in inspector no need to declare any AudioClip . & the reason i did that because that's why many people ad each AudioSource to each game object so thy can have player audio & enemy audio etc... but this way have a trouble when desabling the object that holds the audio

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Footstep sound does not finish playing, it keeps restarting on movement 0 Answers

Second AudioClip won't play 0 Answers

How can I make my audio work for both headphones and computer/laptop speakers? 0 Answers

How to control by code a set of speakers connected to Unity? 0 Answers

Is there a way to get the audio form mobile mic and find it's music key ? Like in a karaoke game. 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