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 imaethan · Oct 11, 2014 at 12:31 PM · javascriptaudioaudioclipjavaaudio.play

Unity audio solution

So I've been looking around and can't find a way for audio to work in my game.

I think I may be going about this in the wrong way so if someone could point me in the right direction that'd be fantastic.

I have the following script, which is set on a cube with an audiosource. I intended to have all the games sounds (excluding music) run on this script/object seeing as they're 2D sounds. When lightning is active I want it to play the sound, which it does, one time. After that, no other sounds work, like the powerup sound or if another lightning object is active.

The soundStop function was just me trying to see if stopping audio after time would help but it didn't. I just can't seem to figure out audio in Unity. Everything else I'm okay with.

 @script RequireComponent(AudioSource)
 var powerUp : AudioClip;
 var playerHit : AudioClip;
 var enemyHit : AudioClip;
 var lightningStrike : AudioClip;
 var lightningStrike2 : AudioClip;
 var enemyDie : AudioClip;
 
 static var waiting : boolean = false;
 
 function Start () {
 
 DontDestroyOnLoad(this);
 
 }
 
 function Update () {
 
 if (PlayerCollision.lightningStrike == true && waiting == false){
 
 waiting = true;
 soundStop();
 //audio.clip = lightningStrike;
 audio.PlayOneShot(lightningStrike);
 
 }
 
 if (PlayerCollision.pickupItem == true && waiting == false){
 
 waiting = true;
 soundStop();
 //audio.clip = powerUp;
 audio.PlayOneShot(powerUp);
 
 }
 
 }
 
 
 function soundWait(){
 
 yield WaitForSeconds(0.1);
 
 waiting = false;
 
 }
 
 function soundStop () {
 
 yield WaitForSeconds(1);
 
 audio.Stop();
 
 }
Comment
Add comment · Show 2
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 imaethan · Oct 11, 2014 at 01:56 PM 0
Share

If you read what I said above the code I specified that I was just trying out audio.Stop. It doesn't have to be there. I don't have 2 of the same ifs, they're for separate collisions in the playerCollision script...

avatar image 767_2 · Oct 11, 2014 at 02:14 PM 0
Share

so you want lightning to be the only sound that plays or something else you didn`t mention what you want

1 Reply

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

Answer by MakeCodeNow · Oct 12, 2014 at 03:19 AM

The main bug is that you're never calling soundWait() and so waiting stays set to true and no other sounds are played.

audio.PlayOneShot() essentially creates a new game object with a new AudioSource on it with the specified clip. That's why your audio.Stop() doesn't work because you're stopping the AudioSource on this object, but the sound effect is on the new object created by PlayOneShot.

PS - Please remember to mark answers as accepted when you are satisfied.

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 imaethan · Oct 12, 2014 at 04:31 PM 0
Share

Thanks! I honestly dunno how I didn't realise I forgot to call the soundWait function. -___-

$$anonymous$$uch appreciated!

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

30 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

Related Questions

JavaScript Play Audiosource Problem 1 Answer

Sound wont play when triggered (javascript) 1 Answer

Playing audio brings this error: An invalid seek position was passed to this function. Does anyone knows what does this mean? 1 Answer

AudioClip won't play?? 0 Answers

Stop audiosource clip from playing 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