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 Snownebula · May 25, 2015 at 09:37 PM · unity 5audioaudiocliplooping

Audio source play stop in Unity5

... I cant get the audio source to play in Unity5. I want to play an audio clip on loop until I tell it to stop. Does anyone know how to make it work? The docs do not make any examples at all. My audio source: var warningToStar : AudioClip;

 if (shipToSun == true) {
             var warningToStar = GetComponent<AudioSource>.Play();
             //audio.Play();
         }
         else if (shipToSun == false) {
             var warningToStar = GetComponent<AudioSource>.Stop();
             //audio.Stop();
         }

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

Answer by Snownebula · May 26, 2015 at 12:51 PM

Thanks guys, but I didn't have internet last night and this is what I came up with or anyone who sees this post down the road. Perhaps someone here did it better than me? I will try yours out and see if it works better :D I am not sure witch is best. var othersun : Transform; var closeDistanceSun = 30;

 var warningToStar : AudioClip;
 var warningTimer : int = 5;
 
 public var shipToSun : boolean = false;
 
 function Start () {
 }
 
 function Update () {
         closeSunWarn();
 }
 
 function closeSunWarn () {
     othersun = GameObject.FindWithTag("t6").transform;
     if (other) {
             var offset = othersun.position - transform.position;
             var sqrLen = offset.sqrMagnitude;
             // square the distance we compare with
             if( sqrLen < closeDistanceSun*closeDistanceSun ) {
                 //print ("The other transform is close to me!");
                 shipToSun = true;
                 }
             else if( sqrLen > closeDistanceSun*closeDistanceSun ) {
                 //print ("The other transform is close to me!");
                 shipToSun = false;
                 }
         }
 }
 function CheckActive1 () {
             
         if (shipToSun == true) {
             warningTimer -= 1;
             if (warningTimer < 1) {
                 GetComponent.<AudioSource>().PlayOneShot(warningToStar);
                 warningTimer = 5;
             }
         }
     }
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 vk007 · May 26, 2015 at 03:54 AM

The audio source has a loop boolean which you can check to get it to loop.alt text

That should make it loop and your code seems correct, so it should stop and play.


screenshot-221.png (18.3 kB)
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
0

Answer by Potote · May 26, 2015 at 03:57 AM

The answer is really simple

You put your play in a selection sentence insteaf of a loop like for or while:

 var warningToStar = GetComponent<AudioSource>();
         
 while (ShipToSun == true){
     warningToStar.Play;
 } 

The problem, is that in a while loop, can execute a lot of time per seconds, y prefer to do a corutine or a invoke method, with the time to loop is the audio clip time:

 var warningToStar = GetComponent<AudioSource>();
      
 public void repeat_method(){
    if (shipToSun == True){
       warningToStar.Play();
       invoke("repeat_method",warningToStar.time);
    }
 }


I hope i can resolve your issue

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 Snownebula · May 26, 2015 at 12:55 PM 0
Share

What would this look like in js?

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

21 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

Related Questions

Looping an AudioClip var... 1 Answer

3D audio source not playing on trigger 0 Answers

Audio/c#/unity Can i control the duration by time ? 1 Answer

Why Won't My Audioclip Loop Seamlessly? 1 Answer

What could make the audio loop? 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