x


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)
more ▼

asked Feb 05 '11 at 04:03 AM

andrew 3 gravatar image

andrew 3
93 4 5 11

maybe if you are using Input.GetKeyDown 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.GetKey. If not, please specify, put your script or a description of what you have do.

Feb 05 '11 at 04:17 AM Uriel_96

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)

Feb 05 '11 at 04:54 PM andrew 3

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

Mar 19 '11 at 11:51 PM AngryOldMan

ok here i add it to the top

Mar 20 '11 at 01:53 AM andrew 3
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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)
more ▼

answered Mar 20 '11 at 07:13 PM

Statement gravatar image

Statement ♦♦
20.2k 35 71 176

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

Mar 20 '11 at 07:15 PM Statement ♦♦

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

Mar 20 '11 at 09:06 PM andrew 3

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 instead. If you still get the error you probably forgot to get rid of the old C# file you created.

Mar 20 '11 at 10:22 PM Statement ♦♦

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.

Mar 20 '11 at 10:25 PM Statement ♦♦

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

Mar 20 '11 at 11:46 PM andrew 3
(comments are locked)
10|3000 characters needed characters left

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)
more ▼

answered Mar 22 '11 at 08:32 PM

andrew 3 gravatar image

andrew 3
93 4 5 11

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

Mar 24 '11 at 11:39 AM Statement ♦♦

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

Mar 25 '11 at 12:18 AM Goody!
(comments are locked)
10|3000 characters needed characters left

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();
}
more ▼

answered Mar 19 '11 at 02:30 PM

efge gravatar image

efge
5.1k 5 14 40

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)

Mar 19 '11 at 03:36 PM andrew 3

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

Mar 20 '11 at 03:00 PM andrew 3

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

Mar 20 '11 at 04:22 PM efge
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Mar 25 '11 at 12:56 AM

Ullukai gravatar image

Ullukai
1 1 1 1

yeah but it dose not work

Mar 25 '11 at 02:14 AM andrew 3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1198
x1060
x304
x285
x145

asked: Feb 05 '11 at 04:03 AM

Seen: 2057 times

Last Updated: Mar 20 '11 at 11:57 AM