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
0
Question by Nerevar1993 · Jul 24, 2013 at 11:17 AM · audiogameaudio.play

Audio on Collision code doesn't work?

Hello, I would like to attach a script on a trigger Mesh (for example a coin), so that when the 1st person player collides with it, the coin will reproduce a sound.

I've tried this code, I've attached it to the object. Obviously the first person controller is tagged with "Player".

 #pragma strict
 
 function OnTriggerEnter(otherObj: Collider)
 {
     if (otherObj.tag == "Player")
     {
         audio.Play();
     } 
     else 
     {
         audio.Stop();
     }
 }

Also i've added an Audio Source Component to the script-attached object, and loaded a WAV sound (2D) inside the Audio Source's Inspector. Anyway when i collide it doesn't reproduces anything. I have the proof that it collides, becouse there's another script attached to the player, so that when he collides with all these similar objects, tagged with the same name (example: coins) it adds 1 point on the score and prints a Text GUI... (Play on awake is disabled)

Please help me, thanks! :)

Comment
Add comment · Show 1
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 amphoterik · Jul 24, 2013 at 11:31 AM 1
Share

Does at least one object in this collision have a rigidbody that is not kinematic? Otherwise, triggers wont work

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by shanth · Jul 24, 2013 at 11:46 AM

First of all make sure that whether your game object is having the component Audio source and drag any of AudioClips to the AudioClip object in that Audio source or assign dynamically using scripting. In your script set your audio.clip to any one of AudioClip's object as below.

C#:

Public AudiClip myClip; // drag your audio clip in unity inspector

void Start() { audio.clip = myClip; }

void OnTriggerEnter(Collider obj){ if(obj.tag == "Player"){ audio.Play(); } else { audio.Stop(); } }

JavaScript:

Var myClip : AudioClip; //drag your audio clip in unity inspector function Start() { audio.clip = myClip; }

function OnTriggerEnter(otherObj: Collider) { if (otherObj.tag == "Player") { audio.Play(); } else { audio.Stop(); } }

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 Nerevar1993 · Jul 24, 2013 at 12:29 PM

So I've changed the script, attached to the Trigger-Mesh, like:

 #pragma strict
 
 var myClip : AudioClip; 
 
 function Start() 
 { 
   audio.clip = myClip; 
   }
  

function OnTriggerEnter(otherObj: Collider) { if (otherObj.tag == "Player")

 { 
 
      audio.Play(); 
 
 } 
 
    else 
 
 { 
 audio.Stop(); 
    } 
 }

On the inspector I've put the audio file, and at the end attached this script to the trigger object, also I've added an Audio Source Component on the same object, and I've added the same audio file on it too. Anyway when I run it, it doesn't reproduce the sound... Here's a link to a screenshot, so you can check if everything is fine: Link to Imageshack

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 shanth · Jul 30, 2013 at 04:36 AM 0
Share

Once confirm that the Player tag object doesn't contain the Audio source and Script, that means the object which contains the script and audio source will check if any other object enters into this object. So the other object tag should be the Player.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Audio plays when i click F 1 Answer

Plugin Container for Firefox/Unity Web Player crash whenever I try to load Dead Trigger 2 0 Answers

Realistic game 1 Answer

Help with walking script 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