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 /
avatar image
0
Question by kenny1007 · Feb 01, 2016 at 01:40 PM · javascriptsound effectsjavascripting

Adding sound to a script trigger

Hello,

I got small Jump scare script that I would like to add sound to it and destroy it after one trigger. I added the variable but i just can't figure out how to get it to work. I would appreciate any help given. :)

Below is the javascript.

 #pragma strict
 
 var Triger_Object : GameObject;
 var Triger_Sound : AudioClip;
 
 function Start () {
 
     Triger_Object.SetActive(false);
 
 }
 
 function OnTriggerEnter () { Triger_Object.SetActive (true); }
 
 function OnTriggerExit () { Triger_Object.SetActive(false); }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cepheid · Feb 01, 2016 at 04:27 PM

@kenny1007

You can use the AudioSource.PlayOneShot() function to play an AudioClip on your object's AudioSource. This is shown in the Scripting API linked below.

AudioSource.PlayOneShot - Unity Scripting API

Hope this helps! :)

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 Cepheid · Feb 01, 2016 at 06:28 PM 0
Share

Simply declare an audioSource reference and play an audioclip through the AudioSource.PlayOneShot() function. It would look like the following:

      var Triger_Object : GameObject;
      var Triger_Sound : AudioClip;
      var Triger_AudioSource : AudioSource;
      
      function Start () {
      
          Triger_Object.SetActive(false);
      
      }
      
      function OnTriggerEnter () { 
          Triger_Object.SetActive (true); 
          Triger_AudioSource.PlayOneShot(Triger_Sound);
      }
      
      function OnTriggerExit () { 
          Triger_Object.SetActive(false); 
          Destroy (Triger_Object);
      }

I hope that clears it up! :)

avatar image kenny1007 Cepheid · Feb 01, 2016 at 07:59 PM 0
Share

Off i see what you did. Bestially telling the script to use Audiosource to play the Audioclip.

I'm not home now but once i get home i'll try this.

avatar image kenny1007 Cepheid · Feb 02, 2016 at 03:47 PM 0
Share

Worked. thx alot. Now i'm going to try to put a timer but that's going to be another question if i can't figure it out first in another post.

:)

avatar image
0

Answer by kenny1007 · Feb 01, 2016 at 06:08 PM

Thx but i'm still no understanding.

I did figured out how to destroy the object but i still can't get the audio to play.

 var Triger_Object : GameObject;
 var Triger_Sound : AudioClip;
 
 function Start () {
 
     Triger_Object.SetActive(false);
 
 }
 
 function OnTriggerEnter () { 
     Triger_Object.SetActive (true); 
 }
 
 function OnTriggerExit () { 
     Triger_Object.SetActive(false); 
     Destroy (Triger_Object);
 }
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

47 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 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

*JavaScript* High Score help (Need help with player prefs) 1 Answer

nullreferenceexception object reference not set to an instance of an object 2 Answers

Unity 5 Sound repeating when detected by raycast JavaScript 2 Answers

Can't add script behaviour TMP_CoroutineTween. The script needs to be derived from MonoBehaviour. I need help with this script. 0 Answers

how to fix menu button "play" 1 Answer


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