• 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
Question by shay4545 · Aug 20, 2014 at 02:48 PM · c#buttonaudioaudioclip

Audio Clip trouble

I have an audio clip that uses AudioSource.PlayClipAtPoint to play when a button is pressed. The button that is pressed makes the scene change, so it only plays part of the audio clip. How would I get the audio clip play fully while still changing scenes (Note: It is only a second or 2 long, I dont want it to keep playing throughout the scene)

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Jeric-Miana · Aug 21, 2014 at 11:45 AM

 var yourAudio : AudioSource;
 
 
 function OnGUI()
     {
     if(GUI.Button(new Rect(10,10,100,100))
     {
     audio.Play();
     ChangeScene();
     }
     }
     function ChangeScene()
     {
     yield WaitForSeconds(10);//delay time to finish your sound
     //then load a scene
     Application.LoadLevel(0);
     
     }

I dont know if this code will help you but i think this will give you an idea to your problem

Comment
zharik86

People who like this

1 Show 5 · 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 shay4545 · Aug 21, 2014 at 02:20 PM 0
Share

I have tried using this in csharp but I have no idea how to use yield.

This is the function I added:

 void PlayAgain()
         {
             yield WaitForSeconds(1);
             Application.LoadLevel(GameplayLevel);
         }

Then I referenced it in the GUI but I have an error saying:

error CS1525: Unexpected symbol (', expecting )', ,', ;', [', or ='

This error is right after "yield WaitForSeconds"

avatar image zharik86 · Aug 21, 2014 at 07:08 PM 0
Share

For coroutine functions in CSharp use:

  IEnumerator PlayAgain() { //change type
   yield return new WaitForSeconds(1.0f); //change return
   Application.LoadLevel(GameplayLevel);
  }
avatar image shay4545 · Aug 21, 2014 at 08:22 PM 0
Share

I added what you said @zharik86 and now the audio plays but the level never loads and I can just click the button as much as I want

avatar image zharik86 · Aug 22, 2014 at 07:12 AM 0
Share

Most likely you incorrectly transferred a code to CSharp. I will write the common method. Below I will write a full code which works:

  public AudioSource myAudio = null; //reference for your audio if it's not component of this object
  //It is possible to make also a variable when pressed the button
  //it is impossible to press more if the audio didn't come to an end yet.
  private bool goAudio = false;


  void OnGUI() {
   if (GUI.Button(new Rect(0, 0, 200, 100), "Load level") && !goAudio) {
    StartCoroutine(this.myPlayAgain());
   }
  }

  IEnumerator myPlayAgain() {
   goAudio = true;
   myAudio.Play();
   //yield return new WaitForSeconds(1.0f);
   //We can find duration of audio and use this value.
   yield return new WaitForSeconds(myAudio.clip.length);
   Application.LoadLevel(GameplayLevel);
   goAudio = false;
  }
avatar image shay4545 · Aug 22, 2014 at 04:20 PM 0
Share

Thanks! it works perfectly!

avatar image

Answer by bustedkrutch · Aug 21, 2014 at 07:19 PM

The way that I'm handling this is that for AudioClips that need to be played across scenes I play them on an AudioSource that is associated with a script on a GameObject that I've used "DontDestroyOnLoad" with.

In my case (for better or worse) I have a "GameController" that survives across all scene changes. That is where I store my "persistent" data. This allows me to fade in the loading scenes ambient "soundtrack" while fading out the exiting scenes "soundtrack:

Let me know if you would like me to expand on this.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make a script that will open a website using default browser? 1 Answer

How to play several audio cllips one after another. 3 Answers

Unity Unable to Reassign Audio Clip 1 Answer

Adding Audio clip in scripting 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


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