- Home /
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)
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.
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)
can you please edit your post to include your script it's just a mess in a comments box
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)
I am still curious about how you control the audio for the gun.
error CS8025: Parsing error that is the error its giving me wtf
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.
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.
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
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(); }
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)
it just gave me a error Assets/NewBehaviourScript.js(7,18): BCE0043: Unexpected token: ..
you should edit line 7 and replace the 3 dots with your random function to assign the actual audio clip.
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)
while (vvv < 0) is going to cause you trouble since it never executes the block.
This is the most difficult to answer question I've seen on Unity answers. 100 points isn't much for this one. ;)
Answer by Meriodoc · Apr 03, 2015 at 09:27 AM
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[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;
}
}
}
Your answer
Follow this Question
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