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)

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)

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

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)

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

[43813-inspector.png*|43813]let 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 *= 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;*
    
  •   }*
    
  • }*
    }