Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 klasklasklas · May 02, 2017 at 01:26 PM · audiosourceaudioclipienumerator

Help with IEnumerator and if sound is playing

I have some problems with my code. First off there is something wrong with my audio.isPlaying-code. It doesnt detect that a sound is already playing. Whats wrong?

And my second problem is that I can get the IEnumerator-code to work. I get a "unexpected symbol"-error. Please help a newbie out... The code is:

 public class ToScene2 : MonoBehaviour {
 
     public AudioClip ljud2;
     private AudioSource audio;
 
     // Use this for initialization
     void Start () {
         audio = Camera.main.GetComponent<AudioSource> ();
 
 
     }
 
 
 
     public void OnClick(){
 
         // If audio isn't already playing, play it.
         if (!audio.isPlaying) {
             
             audio.PlayOneShot (ljud2);
         } 
         else {
             Debug.Log ("A sound is already playing");
         }
 
         IEnumerator waitThreeSeconds()
         {
             yield return new WaitForSeconds(3.0f);
 
             //Go to next scene
             Application.LoadLevel ("scen-rosa");
         }
 
         StartCoroutine(waitThreeSeconds());
 
 
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by sleepandpancakes · May 07, 2017 at 07:40 PM

Place the IEnumerator code outside of the OnClick() function. You can't declare a method inside of another method.

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 Bunny83 · May 07, 2017 at 10:18 PM 0
Share

Yep +1
Nothing to add

avatar image
2

Answer by FM-Productions · May 05, 2017 at 01:06 PM

Hi,

instead of going with audio.PlayOneShot (ljud2);, set the right clip of the audiosource in the Start() function -> audio.clip = ljud2; PlayOneShot is only necessary if you want to play multiple clips on the same audio source.

Then, in onClick, in think this should work.

         if (!audio.isPlaying) {
              
              audio.Play();
          } 

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 klasklasklas · May 07, 2017 at 02:02 PM 0
Share

Thanks for your reply, @eicher It worked!

avatar image FM-Productions · May 08, 2017 at 10:13 PM 0
Share

Good to hear @klasklasklas :) Also, I (and I think everyone else too) would also appreciate an upvote for the answer if it worked.

avatar image klasklasklas FM-Productions · May 09, 2017 at 09:01 AM 0
Share

Sorry, @eicher. I'm a newbie! Done it now. Thanks again.

avatar image Bunny83 · May 09, 2017 at 12:41 AM 0
Share

I upvoted your answer, however i don't really see how the answer is actually related to the question."PlayOneShot" does work as well. The actual error is that he declared his coroutine inside another method. So replacing PlayOneShot(ljud2) with clip = ljud2 doesn't solve the problem.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Movement speed based on audio input 1 Answer

Audio Clip Playing every frame 2 Answers

Do unused audio sources hurt performance? 0 Answers

OnTriggerExit stop audio 1 Answer

Instanced Prefabs: Audio clip array out of range 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