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
-1
Question by Zitoox · Nov 05, 2016 at 03:18 PM · audiosourcescriptingbasicsloopwaitforsecondsplay

Wait before playing audio

I didn't experienced it, but i have seen a lot of users complain about removing the PlayOneShot. As far as i know you used it to play a sound only one time. I want to make a sound be played ONE TIME, wait a few seconds and then play again in a loop: play,wait,play,wait,play,wait, etc...

But how do i do that? I made this for now but it doesn't seems to work:

 #pragma strict
 
 var Enemy : Transform;
 var distance = 150;
 var Signal : AudioSource;
 private var Close = false;
 var timer : float; //The time it was supposed to wait.
 
 
 function Update()
 {
 
     if(Vector3.Distance(transform.position, Enemy.position) < distance)
     {
         Close = true;
         Signal.Play(timer); //Doesn't wait.
         //Can't add yield wait for seconds here because it's in an Update function.
         
     }
     else
     {
         Close = false;
     }
 }
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
0

Answer by Kapel · Nov 05, 2016 at 05:03 PM

Use AudioSource.isPlaying to detect if playing is done then start Coroutine and again AudioSource.Play()

I hope this code i made special for you will explain you how to do this.

 using UnityEngine;
 using System.Collections;
 
 public class AudioS : MonoBehaviour {
     bool PlaySound;
 
     // Use this for initialization
     void Start () {
         PlaySound = true;
     }
     
     // Update is called once per frame
     void Update () {
         if (PlaySound == true) 
         {
             GetComponent<AudioSource>().Play();
             PlaySound = false;
         }
         if(GetComponent<AudioSource>().isPlaying == false)
         {
             StartCoroutine(Wait());
         }
     }
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(2.0f);
         PlaySound = true;
     }
 }

You can also use Invoke instead of coroutine.

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 Zitoox · Nov 05, 2016 at 08:58 PM -1
Share

There are some errors in the script refering to the PlaySound

avatar image Kapel · Nov 06, 2016 at 12:11 AM 0
Share
 using UnityEngine; 
 using System.Collections;
 
 public class AudioS : $$anonymous$$onoBehaviour {
 bool PlaySound;
 
  // Use this for initialization
  void Start () {
      PlaySound = true;
  }
  
  // Update is called once per frame
  void Update () {
      if (PlaySound == true) 
      {
          GetComponent<AudioSource>().Play();
          PlaySound = false;
      }
      if(GetComponent<AudioSource>().isPlaying == false)
      {
          StartCoroutine(Wait());
      }
  }
  IEnumerator Wait()
  {
      yield return new WaitForSeconds(2.0f);
      PlaySound = true;
  }

Impossible, Because this code works for me without any errors. In your code i see you are using JS and my code is C#. Be sure you paste this code to C# file or adapt it for JS.

avatar image Zitoox Kapel · Nov 06, 2016 at 01:15 AM -1
Share

Yeah, actually now it worked! I think it is because my engine is bugging and my Scripts are being ignored and all sorts of things don't ask me why.

But now i've realised something... Where do this fit in my script? Like... How am i supposed to use this in my script? I need my script's basic functions and this, not just play a sound =\

avatar image Kapel Zitoox · Nov 06, 2016 at 11:29 AM 0
Share

It should be something like that or simillar

  if(Vector3.Distance(transform.position, Enemy.position) < distance && PlaySound == true)
 {
  GetComponent<AudioSource>().Play();
           PlaySound = false;
 }
 
  if(GetComponent<AudioSource>().isPlaying == false)
       {
           StartCoroutine(Wait());
       }
 
 // Rest of your code
 
 ...
 
 }
 
   IEnumerator Wait()
   {
       yield return new WaitForSeconds(2.0f);
       PlaySound = true;
   }
avatar image
0

Answer by vulgerstal · Nov 05, 2016 at 10:26 PM

 using UnityEngine;
 using System.Collections;
 
 public class vulgerstalAudioLoopDelayed : MonoBehaviour {
 
     public float delay;
     float delayTemp;
 
     // Use this for initialization
     void Start () {
         delayTemp = delay;
     }
     
     // Update is called once per frame
     void Update () {
         if (GetComponent<AudioSource>().isPlaying)
             delay = delayTemp;
         else
             delay -= Time.deltaTime;
 
         if (delay <= 0)
             GetComponent<AudioSource>().Play();
 
     }
 }
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

62 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 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 avatar image avatar image

Related Questions

Loop animation with a wait in between 3 Answers

Instantiate color change loop 0 Answers

Yield WaitForSeconds Not Working. Coroutine. 1 Answer

Audio clip not playing 1 Answer

Playing Sound 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