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 Butterzroxx · Sep 30, 2011 at 07:47 AM · triggersoundontriggerenteraudioplay

Sound On Collision Not Working

What I'm trying to do is have a sound file play when my player walks through a doorway. I added a game object, removed the mesh, clicked is trigger, added this script::

var audio : AudioClip;

function OnTriggerEnter (other : Collider) {

 audio.clip = audio;

 audio.Play();

}

And no matter what tutorial I watch or read it all fails. I get no sound no matter what script I use. I have also tried using other file formats for the sound and still nothing. I am completely lost on what I am missing.

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

Answer by TheEmeralDreamer · Sep 30, 2011 at 08:43 AM

It's a pretty simple fix actually. The only problem with your code is that you would need to tell the audio which sound to play. var audiox:AudioClip=whateversoundyouwanthere;//note:you may need to drag this file to the var in the inspector window audio.Play(audiox);

Altho I recommend using PlayOneShot of the audio usage.
This will enable you to play more than one sound at a time, should you need to. I usually just put it into a function like one of these:

function PlayAudio(newClip:AudioClip,volume:float) { audio.PlayOneShot(newClip,volume); }

function PlayAudioDual(newClip:AudioClip,newClip2:AudioClip,volume:float)
{
audio.PlayOneShot(newClip,volume);
audio.PlayOneShot(newClip2,volume);
}

then you can just call it like this:

PlayAudio(audiox,1.0);

Finally, last but not least:

If you don't get any SOUND at all(like the sounds aren't playing in game), you need to make sure you have an audio SOURCE as well as an audio LISTENER in your scene. Otherwise you won't hear sounds period(just thought it worth mentioning altho I don't think that's your problem.)

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 Kacer · Sep 30, 2011 at 08:20 AM

In C# at least there is already a variable that is named "audio", so try writing it like this:

 var myClip : AudioClip;
 
 function OnTriggerEnter(other : Collider){
      audio.clip = myClip;
      audio.Play();
 }

remember to assign the audio clip you want to play from the inspector, and remember to put an audiosource on the object you've got this script on.

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 Butterzroxx · Oct 01, 2011 at 03:08 AM

Thank you guys for the help I found the issue. My game is in 3rd Person so I had the Listener on the camera instead of the player. So I switched it around and put the Listener on the player and took if off the camera and it worked. I feel like an idiot <.<

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter gives me error? 2 Answers

Disable Mesh Collider Trigger 1 Answer

Triggering random sound on player 1 Answer

OnTriggerEnter fails to activate 1 Answer

Totally new noob to mecanim and unity itself - Trigger animation? 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