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 BabyDev · Jul 16, 2014 at 11:46 AM · audiosoundnot workingplayplaying

Can't get my sound to work..

Hey everyone!

I am having a problem with playing my sounds. So basically my sound sometimes does play and it sometimes doesn't play.

What I did so far is create a bool that checks if the sound is played already and i've created another if statement to make sure audio.play is only in the code once. Previously I didn't have this if statement and just copied and paste the audio.play two times in my other 2 if statements and that didn't work either.

Any help would be much apreciated!

This is my code so far:

 void OnGUI() {
     GUI.skin = customSkin;

     //Show current bacons.
     GUI.Label (new Rect(camera.pixelWidth / 20, camera.pixelHeight / 40, camera.pixelWidth / 5, camera.pixelHeight / 7), "<size='" + (camera.pixelWidth / 20) + "'>" + PlayerPrefs.GetInt ("BaconTotal") + "</size>");

     GUI.Box (new Rect(camera.pixelWidth / 5, - (camera.pixelHeight / 4), (camera.pixelWidth / 4 * 3), (camera.pixelHeight / 5 * 4)), "", customSkin.GetStyle("Logo"));

     if(GUI.Button (new Rect(camera.pixelWidth / 5 * 2, (camera.pixelHeight / 5), (camera.pixelWidth / 5), (camera.pixelHeight / (625 / 100))), "", customSkin.GetStyle("Play"))) {
         loadLevel = 1;
         buttonPressed = true;
     }
     else if(GUI.Button (new Rect(camera.pixelWidth / 5 * 2, (camera.pixelHeight / 2) - (camera.pixelHeight / 10), (camera.pixelWidth / 5), (camera.pixelHeight / (625 / 100))), "", customSkin.GetStyle("Store"))) {
         loadLevel = 2;
         buttonPressed = true;
     }

     if(buttonPressed && PlayOnce == false) {
         Application.LoadLevel(loadLevel);
         audio.Play();
     }
 }
Comment
Add comment · Show 3
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 BabyDev · Jul 16, 2014 at 10:58 AM 0
Share

So a little update. Firstly I must state that the sound sometimes does play and sometimes doesn't and secondlyI posted the wrong code :P. Here is my code:

void OnGUI() { GUI.skin = customSkin;

     //Show current bacons.
     GUI.Label (new Rect(camera.pixelWidth / 20, camera.pixelHeight / 40, camera.pixelWidth / 5, camera.pixelHeight / 7), "<size='" + (camera.pixelWidth / 20) + "'>" + PlayerPrefs.GetInt ("BaconTotal") + "</size>");

     GUI.Box (new Rect(camera.pixelWidth / 5, - (camera.pixelHeight / 4), (camera.pixelWidth / 4 * 3), (camera.pixelHeight / 5 * 4)), "", customSkin.GetStyle("Logo"));

     if(GUI.Button (new Rect(camera.pixelWidth / 5 * 2, (camera.pixelHeight / 5), (camera.pixelWidth / 5), (camera.pixelHeight / (625 / 100))), "", customSkin.GetStyle("Play"))) {
         loadLevel = 1;
         buttonPressed = true;
     }
     else if(GUI.Button (new Rect(camera.pixelWidth / 5 * 2, (camera.pixelHeight / 2) - (camera.pixelHeight / 10), (camera.pixelWidth / 5), (camera.pixelHeight / (625 / 100))), "", customSkin.GetStyle("Store"))) {
         loadLevel = 2;
         buttonPressed = true;
     }

     if(buttonPressed && PlayOnce == false) {
         PlayOnce = true;
         audio.Play();
         Application.LoadLevel(loadLevel);
     }
 }
avatar image $$anonymous$$ · Jul 16, 2014 at 11:53 AM 0
Share

The problem is that as soon the sound start it also load the level thats why it gets cut, before loading the level you have to wait for the sound to finish or it will be random depending on how much time it gets to load the next level.

Try to change Application.LoadLevel(loadLevel); to

if(!audio.isPlaying) Application.LoadLevel(loadLevel);

So in this way first plays the audio and when is done proceed to load the level.

avatar image awest · Jul 16, 2014 at 11:55 AM 0
Share

Have you tried commenting out the load level on line 18. It may be playing and then immediately stopping when it loads the next level.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BabyDev · Jul 16, 2014 at 04:37 PM

Allright so I exported to an .Apk and on my android phone it works perfectly. Wierd that it doesn't work within Unity then.

Thanks anyways!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Audio files not playing at all 4 Answers

Audio Not Always Playing 3 Answers

Background Music 2 Answers

Audio wont play when conditions are met 1 Answer

Play An Audio Clip When An Instantiated Object Collides 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