Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 thornekey · Jan 05, 2014 at 01:10 PM · c#audio source3d sound

Audio not playing

I have an issue, where the audio wont play. its on a spot light object inside a camera. the spot light has the following script and has audio source components and the player object (houses the camera) has a audio listener. YET I HEAR NO SOUND...

 using UnityEngine;
 using System.Collections;
 
 public class torchscript : MonoBehaviour {
 
 public bool on = false;
 public AudioClip FlashOff;
 
     
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.F))
         on = !on;
     if(on) 
         light.enabled = true;
 
         else if(!on) 
         light.enabled = false;
         audio.PlayOneShot(FlashOff, 0.7F);
 
     }
 }


i know theres been a lot of problems with this for people before..

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 thornekey · Jan 05, 2014 at 10:54 PM 0
Share

bump please help

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ArkaneX · Jan 06, 2014 at 01:26 AM

I think the problem is that you call PlayOneShot outside of your if. So it is effectively called 60 times a second (assuming your game runs at 60FPS). Please include it inside of the if statement by introducing curly brackets:

 if(on)
 {
     light.enabled = true;
 }
 else
 {
     light.enabled = false;
     audio.PlayOneShot(FlashOff, 0.7F);
 }

Of course only curly brackets after else are necessary. The pair after if can be omitted, but I strongly suggest to always use them - this IMO makes your code more readable. Besides, there's no need to use

 else if(!on)

because in this case just else is enough.

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 thornekey · Jan 06, 2014 at 05:21 AM 0
Share

ok so it seems to work now. kind of. It does play in the editor but when i compile and play it, it does... YET it plays constantly from the launch of the game.. idk why..

avatar image Invertex · Jan 06, 2014 at 05:47 AM 1
Share

Did you encapsulate it all within the Get$$anonymous$$eyDown statement? If not, that would be why, since it doesn't need to meet that condition to check if the light is off and play the audio. But you can simplify the code even more like this.

 void Update()
 {
     if ((Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.F)) && (!on)) 
     {
         on = true;
         light.enabled = true;
     }
     else if((Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)) && (on))
     {
         on = false;
         light.enabled = false;
         audio.PlayOneShot(FlashOff, 0.7F);
     }
 }

You could make it even simpler by getting rid of the "on" bool and just using "light.enabled in the conditions above.

avatar image thornekey · Jan 06, 2014 at 06:30 AM 0
Share

thank you this has worked.

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

20 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

Related Questions

Unity 5 Audio Source volume falloff for mono sound without panning 0 Answers

How to convert a 2d sound to a 3d sound in script? 0 Answers

Different music for pause menu? 1 Answer

How to play music/audio 2 Answers

How do I play audio once in Update? 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