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
2
Question by andrew 3 · Feb 05, 2011 at 04:03 AM · audiofpsshootingloopstop

my audio is not looping even when it is set to loop

when i shoot my gun in the play bar it makes the gun shot noise when i shoot but after the seventh shot it no longer plays the audio can some one tell me what is wrong thank you!

thanks but that did not work but here is the script im using:

// Plays back one of the audio choosing randomly between them. var clips : AudioClip[] = new AudioClip[1];

function Start () { DontDestroyOnLoad(this); audio.loop = true; while (true) { audio.clip = clips[Random.Range(0, clips.length)]; audio.Play();
if (audio.clip) yield WaitForSeconds(audio.clip.length); else yield; } }

@script RequireComponent(AudioSource)

Comment
Add comment · Show 4
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 Uriel_96 · Feb 05, 2011 at 04:17 AM 0
Share

maybe if you are using Input.Get$$anonymous$$eyDown you will note this that it only going to do one time, if this is your case I recommend you to change it to Input.Get$$anonymous$$ey. If not, please specify, put your script or a description of what you have do.

avatar image andrew 3 · Feb 05, 2011 at 04:54 PM 0
Share

here is the script i have been using // Plays back one of the audio choosing randomly between them. var clips : AudioClip[] = new AudioClip[1];

function Start () { DontDestroyOnLoad(this); audio.loop = true; while (false) { audio.clip = clips[Random.Range(0, clips.length)]; audio.Play();
if (audio.clip) yield WaitForSeconds(audio.clip.length); else yield; } }

@script RequireComponent(AudioSource)

avatar image AngryOldMan · Mar 19, 2011 at 11:51 PM 1
Share

can you please edit your post to include your script it's just a mess in a comments box

avatar image andrew 3 · Mar 20, 2011 at 01:53 AM 0
Share

ok here i add it to the top

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Mar 20, 2011 at 07:13 PM

I wasn't able to reproduce your issue. Your method "works" but setting them looping cause sound glitches. Here's a rewrite of the same code. I tested it with two different sounds and it works good for me. All I did was slap this and an AudioSource on a game object, together with two clips for the script. Instead of waiting for a timer I am checking until the sound stops. I don't use looping sounds (since this will do the loop by itself). Also I call audio.Stop() between each clip as you can see below, but I doubt it would do any difference unless you were waiting and using a looping sound (as you were originally).

// This code was tested and seems to run fine on my machine.

// Plays back one of the audio choosing randomly between them. var clips : AudioClip[] = new AudioClip[1];

function Start () { DontDestroyOnLoad(this);

 audio.playOnAwake = false;
 audio.loop = false;

 while (true) 
 { 
     audio.clip = clips[Random.Range(0, clips.length)]; 
     audio.Play();

     while (audio.isPlaying)
         yield;

     audio.Stop();
 } 

}

@script RequireComponent(AudioSource)

Comment
Add comment · Show 6 · 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 Statement · Mar 20, 2011 at 07:15 PM 0
Share

I am still curious about how you control the audio for the gun.

avatar image andrew 3 · Mar 20, 2011 at 09:06 PM 0
Share

error CS8025: Parsing error that is the error its giving me wtf

avatar image Statement · Mar 20, 2011 at 10:22 PM 0
Share

It's because you put JavaScript code in a C# file! You must create a JavaScript file and put the code there. If you are unsure about how, just right click in the project files list and select Create/JavaScript. Then paste the code in that file ins$$anonymous$$d. If you still get the error you probably forgot to get rid of the old C# file you created.

avatar image Statement · Mar 20, 2011 at 10:25 PM 0
Share

Basically there are three different languages that Unity3D supports. It's important you put the right code in the right files. C# code go in .cs files. JavaScript (also called UnityScript) goes in .js files. Boo (Python, or similar to python, I never used it) code go into .boo files.

avatar image andrew 3 · Mar 20, 2011 at 11:46 PM 0
Share

very interesting when i had while(true) it looped like it was suppose to but i had know control of the gun making the noise when i wanted to but when i put in while(false) i had control when i wanted it to make the gun shot noise but after for shots it stopped even though its a mag of 6 bullets

Show more comments
avatar image
0

Answer by efge · Mar 19, 2011 at 02:30 PM

The conditional statement must be True for the while loop's code to be executed, but in your script there is:

  while (false)
  ... 


Edit: Maybe you should use this script and assign the clip when the player presses a button:

function Start () { audio.loop = true; }

function Update () { if (Input.GetKeyDown (KeyCode.DownArrow)) //assign your key here audio.clip = ...; //your random function audio.Play(); if (Input.GetKeyUp (KeyCode.UpArrow)) //assign same key here audio.Stop(); }

Comment
Add comment · Show 3 · 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 andrew 3 · Mar 19, 2011 at 03:36 PM 0
Share

thanks but that did not work but here is the script im using // Plays back one of the audio choosing randomly between them. var clips : AudioClip[] = new AudioClip[1]; function Start () { DontDestroyOnLoad(this); audio.loop = true; while (true) { audio.clip = clips[Random.Range(0, clips.length)]; audio.Play(); if (audio.clip) yield WaitForSeconds(audio.clip.length); else yield; } } @script RequireComponent(AudioSource)

avatar image andrew 3 · Mar 20, 2011 at 03:00 PM 0
Share

it just gave me a error Assets/NewBehaviourScript.js(7,18): BCE0043: Unexpected token: ..

avatar image efge · Mar 20, 2011 at 04:22 PM 0
Share

you should edit line 7 and replace the 3 dots with your random function to assign the actual audio clip.

avatar image
0

Answer by andrew 3 · Mar 22, 2011 at 08:32 PM

where here is the code im using now this one my uncle gave me (he is a programer for a living) and it still stops at four shots we know its not the script and it is not a audio listener problem because i only audio lister in the level

// Plays back one of the audio choosing randomly between them. 
var clips : AudioClip[] = new AudioClip[1]; 
function Start () 
{ 
    DontDestroyOnLoad(this); 
    audio.playOnAwake = false; 
    audio.loop = false; 
    var vvv =0; 
    while (vvv<0) 
    { 
        audio.clip = clips[Random.Range(0, clips.length)]; 
        audio.Play(); 
        while (audio.isPlaying) 
            yield; 
        audio.Stop(); 
        vvv++; 
    } 
} 
@script RequireComponent(AudioSource)

Comment
Add comment · Show 2 · 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 Statement · Mar 24, 2011 at 11:39 AM 0
Share

while (vvv < 0) is going to cause you trouble since it never executes the block.

avatar image Goody! · Mar 25, 2011 at 12:18 AM 0
Share

This is the most difficult to answer question I've seen on Unity answers. 100 points isn't much for this one. ;)

avatar image
0

Answer by Ullukai · Mar 25, 2011 at 12:56 AM

can't you just set it to loop in the inspector ?

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 andrew 3 · Mar 25, 2011 at 02:14 AM 0
Share

yeah but it dose not work

avatar image
0

Answer by Meriodoc · Apr 03, 2015 at 09:27 AM

alt textlet me explain anyway. I needed to loop a audio clip (use wav...not mp3). my clip runs at 100bpm...So all i did was change the bpm in the inspector and added the clip twice in the list also in the inspector. And i set this in the script to a small value: AudioSettings.dspTime + 0.01F; Perfect looping sound. Hope this helps someone, because the looping function in unity in limited.

using UnityEngine; using System.Collections;

[RequireComponent(typeof(AudioSource))] public class AudioJacques : MonoBehaviour {

 public float bpm = 140.0F;
 public int numBeatsPerSegment = 16;
 public AudioClip[] clips = new AudioClip[2];
 private double nextEventTime;
 private int flip = 0;
 private AudioSource[] audioSources = new AudioSource[2];
 private bool running = false;
 void Start() {
     int i = 0;
     while (i < 2) {
         GameObject child = new GameObject("Player");
         child.transform.parent = gameObject.transform;
         audioSources[i] = child.AddComponent<AudioSource>();
         i++;
     }
     nextEventTime = AudioSettings.dspTime + 0.01F;
     running = true;
 }
 void Update() {
     if (!running)
         return;
     
     double time = AudioSettings.dspTime;
     if (time + 1.0F > nextEventTime) {
         audioSources[flip].clip = clips[flip];
         audioSources[flip].PlayScheduled(nextEventTime);
         Debug.Log("Scheduled source " + flip + " to start at time " + nextEventTime);
         nextEventTime += 60.0F / bpm * numBeatsPerSegment;
         flip = 1 - flip;
     }
 }

}


inspector.png (42.9 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

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

Stop audio loop 1 Answer

Stop sound on input.getkeyup 1 Answer

Pause Menu Audio 1 Answer

Is there a way to loop mp3 files seamlessly? 2 Answers

Raycasr in my fps? 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