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 nicole · Sep 06, 2010 at 03:10 AM · guibuttonaudiosoundloadlevel

Play sound on button click before loading level.

I've looked through most of the similar questions here, and tried different variations of my code, but I'm still unable to get it to work right.

Just to make it clear, I want a sound to be heard when I click my button before it loads to a level.

The code I'm currently using is:

var start : GUIStyle; var quite : GUIStyle; var instruction : GUIStyle; var history : GUIStyle;

var mySound : AudioClip;

function OnGUI () { if(GUI.Button (Rect(800,260,250,160),"",start)) { audio.Play(); if (!audio.isPlaying) { Application.LoadLevel ("Proj"); } }

if(GUI.Button (Rect(250,600,240,130),"",quite)) { audio.Play(); if (!audio.isPlaying) { Application.Quit (); } }

if(GUI.Button (Rect(790,520,250,130),"",instruction)) { audio.Play(); if (!audio.isPlaying) { Application.LoadLevel ("instruction"); } }

if(GUI.Button (Rect(240,270,290,140),"",history)) { audio.Play(); if (!audio.isPlaying) { Application.LoadLevel ("History"); } }

}

This only allows an audio clip to be played upon clicking the button, but it doesn't load the respective levels. When I use this code:

if(GUI.Button (Rect(800,260,250,160),"",start)) { audio.Play(); Application.LoadLevel ("Proj"); }

if(GUI.Button (Rect(250,600,240,130),"",quite)) { audio.Play(); Application.Quit (); }

if(GUI.Button (Rect(790,520,250,130),"",instruction)) { audio.Play(); Application.LoadLevel ("instruction"); }

if(GUI.Button (Rect(240,270,290,140),"",history)) { audio.Play(); Application.LoadLevel ("History");

The levels are loaded, but the audio clips are not played. Can anyone tell me where I went wrong?

Thank you!

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

Answer by · Sep 06, 2010 at 03:55 AM

In your first example, the audio is playing when it gets to the check (before loading the level).

function OnGUI ()
{
    if(GUI.Button (Rect(800,260,250,160),"",start))
    {
        audio.Play();           // audio is told to play
        if (!audio.isPlaying)   // audio is now playing, so this does not execute
        {
        Application.LoadLevel ("Proj");
        }
    }

It only evaluates that !audio.isPlaying once, when the button is clicked.

You should use a Coroutine. For example:

function OnGUI ()
{
   if(GUI.Button (Rect(800,260,250,160),"",start))
   {
      StartLevel();
   }

and outside of the OnGUI function:

function StartLevel ()
{
   audio.Play();                    // play audio
   while (audio.isPlaying)          // while audio is playing,
   {
      yield;                        // chill out in here.
   }
   Application.LoadLevel ("Proj");  // then, continue on.
}

If there's anything you don't understand, please ask. Hope this helps!

Comment
Add comment · Show 4 · 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 · Sep 06, 2010 at 03:58 AM 0
Share

You could improve this further by adding a private bool that would only allow the StartLevel function to be called if it hadn't already. You'd need to set it to true at the top of StartLevel(), and back to false when the LoadLevel function call is made.

avatar image nicole · Sep 06, 2010 at 05:29 AM 0
Share

Gracias, I understand the script, & it works perfectly fine!

avatar image mattskate96 · Apr 06, 2012 at 12:45 AM 0
Share

hey, everything works fine for me, except the sound plays at the beginning of the scene. I copied the exact same script as your coroutine. Any ideas?

avatar image c.ovayurt · Feb 19, 2015 at 01:44 PM 0
Share

remove tick on "Play On Awake" in Audio Source from Inspector

avatar image
3

Answer by s_p_oneil · Jun 08, 2011 at 03:06 PM

I believe I found a better solution. If you look at the docs for PlayClipAtPoint, it says: "If you want further control over playback, you can use the following code instead." The code is like this:

 AudioSource PlayAudioClip(AudioClip clip, Vector3 position, float volume) {
     GameObject go = new GameObject("One shot audio");
     go.transform.position = position;
     AudioSource source = go.AddComponent<AudioSource>();
     source.clip = clip;
     source.volume = volume;
     source.Play();
     Destroy(go, clip.length);
     return source;
 }

All you need to do to get the sound to keep playing when you load the level is call "Object.DontDestroyOnLoad(go)" before you return the AudioSource. I've tested it, and it solved the problem for me.

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
1

Answer by jtbentley · Sep 07, 2010 at 12:54 PM

yes, ideally something like this :)

yield WaitForSeconds(audio.length);
Application.LoadLevel("blah");
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 Anni2D · Aug 21, 2014 at 02:12 PM 0
Share

FYI, it is audio.clip.length, not audio.length :)

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

1 Person is following this question.

avatar image

Related Questions

Play Sound when GUI button is pressed. 7 Answers

Loading scene with a prefab GUI button? 1 Answer

play an audio clip from a GUI button 1 Answer

Button doesn't play sound after different button press 0 Answers

Sound to play when clicking buttons on GUI 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