- Home /
Why Doesn't My Music Loop?
I have an audio clip that plays when my game starts up, but it doesn't loop. I'm not sure why it doesn't loop.
Here's the code exactly as it stands, it's not too complicated.
#pragma strict
function Start () {
AudioSource.PlayClipAtPoint(audio.clip, Camera.main.transform.position);
}
function Update () {
// Play the audio for this image
if (audio.isPlaying != true)
AudioSource.PlayClipAtPoint(audio.clip, Camera.main.transform.position);
Debug.Log(audio.loop);
}
The Debug.Log line is reporting true. My music ends, and the sound does not get played again. How do I get my sound to loop?
Answer by hijinxbassist · Mar 21, 2012 at 09:02 AM
Have you actually physically checked that the audio sources loop is set to true(box with check mark). If not, un click "Maximize On Play" and look at the offending audio source while the game is playing.
Also, keep looking at the audio source after the sound has finished. This will help deter$$anonymous$$e what is wrong. Just for testing, add this new line to your script....
function Start ()
{
AudioSource.PlayClipAtPoint(audio.clip, Camera.main.transform.position);
transform.GetComponent(AudioSource).audio.loop=true;
}
Is the source parented to your camera, if not...then this is the problem. There needs to be an AudioListener in range of the AudioSource. If this is its own GameObject, parent it to the camera. Simple as that, the more i think about it, the more i think this is the issue. You have it play at the cameras position, but then you move and no longer hear it(i cant say for sure without seeing actual game play).
Thanks, attaching the audio source and script to my camera -- ins$$anonymous$$d of my background game object -- made it actually loop after the sound finished playing.
I'm also really happy that this led to me being able to use "audio.Play()" as advertised. It wouldn't work, before, likely because of some camera hijinx that I don't know about.
Please don't reply to a 2 year old thread. Ins$$anonymous$$d, make your own. :)
Don't use Debug.Log() for this because it will keep echoing over-and-over and dropping the framerate.
Answer by Kryptos · Mar 21, 2012 at 04:36 PM
You can't loop sounds started with AudioSource.PlayClipAtPoint. This method doesn't use the AudioSource component of your object but instead creates a temporary (and hidden) object. This is the intended behaviour.
As mentioned in documentation, you can use an alternative method. See http://unity3d.com/support/documentation/ScriptReference/AudioSource.PlayClipAtPoint.html for more details.
Answer by theropodx · Jun 29, 2012 at 02:12 AM
Strange, the "alternative method" from that link has disappeared. Maybe Unity removed that function in the latest build? Anyway, it's missing from the doc link above now.
Answer by Vetpetmon · Nov 29, 2014 at 02:09 AM
When I used the code it would also try to crash my computer with it spamming 379 messages saying "True" per 30 seconds, any fixes? It lagged so badly it dropped me down to 0 Frames Per Second, I spent 10 minuets to restore my computer's memory usage to normal. EDIT: Now it's spamming the music! EDIT:IT WORKS!!!:
#pragma strict
function Start () {
AudioSource.PlayClipAtPoint(audio.clip, Camera.main.transform.position);
transform.GetComponent(AudioSource).audio.loop=true;
}
function Update () {
// Play the audio for this image
if (audio.isPlaying != true)
AudioSource.PlayClipAtPoint(audio.clip, Camera.main.transform.position);
}
Thanks, that helped a lot.
Answer by kfirmon · Oct 03, 2015 at 02:38 PM
The problem might be a result of using "Play on awake". I attached a script to the Audio Source instead and it solved the problem.
using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour
{
public AudioClip clip; // Drag the clip here on the editor
private AudioSource au;
void Start()
{
au = GetComponent<AudioSource>();
au.loop = true;
au.Play();
}
}
Your answer
Follow this Question
Related Questions
Trigger audio loop on beat with PlayScheduled 1 Answer
Background music 0 Answers
How do i add/import sound effects and/or music into my game? 5 Answers
Can't get a sound to loop 2 Answers