mantasp, thanks for replying!
I did indeed see that other thread you linked to, which is why I tried my game with both compressed and uncompressed audio to see if that was the problem.
How I’m playing the sounds in my game is that I have an empty game object with both an audio source and audio listener attached, as well as my “PlaySounds” script.
Once in the game, while the player is aiming their shot I have some ambient nature sounds playing as a background. When an event occurs such as the player firing or their projectile hitting a target or the ground, etc…, those objects (ground, projectile, target, etc…) call a function in the “PlaySounds” script to play the appropriate sound, before returning to the default nature sounds.
Here is my “PlaySounds” script:
private var quiverScript : QuiverScript;
var playThis : AudioClip;
var menuAudio : AudioClip; //0
var aimingAudio : AudioClip; //1
var scoreAudio : AudioClip; //2
var missAudio : AudioClip; //3
var arrowAudio : AudioClip; //4
var onePointAudio : AudioClip; //5
var threePointAudio : AudioClip; //6
var fivePointAudio : AudioClip; //7
var appleAudio : AudioClip; //8
function Awake () {
audio.clip = menuAudio;
//playThis = menuAudio;
audio.Play();
audio.loop = true;
}
function Start () {
** var scoreObject : GameObject;**
** scoreObject = GameObject.Find(“ScoreKeeper”);**
** quiverScript = scoreObject.GetComponent(QuiverScript);**
}
function Update () {
** print(audio.clip.name.ToString());**
}
function ClipPlay (playThis : int) {
** if (playThis == 0) {**
** audio.clip = menuAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (playThis == 1) {**
** audio.clip = aimingAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (playThis == 2) {**
** audio.clip = scoreAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (playThis == 3) {**
** audio.clip = missAudio;**
** audio.loop = false;**
** audio.Play();**
** yield WaitForSeconds (audio.clip.length);**
** if (quiverScript.arrowsLeft == 0) {**
** audio.clip = scoreAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (quiverScript.arrowsLeft > 0) {**
** audio.clip = aimingAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** }**
** } else if (playThis == 4) {**
** audio.clip = arrowAudio;**
** audio.loop = false;**
** yield new WaitForSeconds(0.1);**
** audio.Play(); **
** } else if (playThis == 5) {**
** audio.clip = onePointAudio;**
** audio.loop = false;**
** yield new WaitForSeconds(0.1);**
** audio.Play(); **
** yield WaitForSeconds (audio.clip.length);**
** if (quiverScript.arrowsLeft == 0) {**
** audio.clip = scoreAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (quiverScript.arrowsLeft > 0) {**
** audio.clip = aimingAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** }**
** } else if (playThis == 6) {**
** audio.clip = threePointAudio;**
** audio.loop = false;**
** yield new WaitForSeconds(0.1);**
** audio.Play(); **
** yield WaitForSeconds (audio.clip.length);**
** if (quiverScript.arrowsLeft == 0) {**
** audio.clip = scoreAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (quiverScript.arrowsLeft > 0) {**
** audio.clip = aimingAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** }**
** } else if (playThis == 7) {**
** audio.clip = fivePointAudio;**
** audio.loop = false;**
** yield new WaitForSeconds(0.1);**
** audio.Play(); **
** yield WaitForSeconds (audio.clip.length);**
** if (quiverScript.arrowsLeft == 0) {**
** audio.clip = scoreAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (quiverScript.arrowsLeft > 0) {**
** audio.clip = aimingAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** }**
** } else if (playThis == 8) {**
** audio.clip = appleAudio;**
** audio.loop = false;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** yield WaitForSeconds (audio.clip.length);**
** if (quiverScript.arrowsLeft == 0) {**
** audio.clip = scoreAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (quiverScript.arrowsLeft > 0) {**
** audio.clip = aimingAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** }**
** }**
}
And here is an excerpt from the “GroundHit” script calling the “ClipPlay” function from the “PlaySound” script:
private var playSounds : PlaySounds;
function Start() {
var soundObject : GameObject;
soundObject = GameObject.Find(“AudioPlayer”);
playSounds = soundObject.GetComponent(PlaySounds);
}
function OnCollisionEnter(hit : Collision) {
** playSounds.ClipPlay(3);**
** screenNav.screenShown.texture = screenNav.missShot;**
** yield new WaitForSeconds(2);**
** playSounds.ClipPlay(1);**
}
So as you’ll remember from the earlier code, calling the function “ClipPlay” with the (3) next to it would execute this code from “PlaySounds” (note that in order to avoid having two clips play at once–even if they are uncompressed audio just to be safe–I wait for the audio.clip.length to fully play before switching back to the ambient nature sounds which is called “aimingAudio” below):
} else if (playThis == 3) {
** audio.clip = missAudio;**
** audio.loop = false;**
** audio.Play();**
** yield WaitForSeconds (audio.clip.length);**
** if (quiverScript.arrowsLeft == 0) {**
** audio.clip = scoreAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** } else if (quiverScript.arrowsLeft > 0) {**
** audio.clip = aimingAudio;**
** audio.loop = true;**
** yield new WaitForSeconds(0.1);**
** audio.Play();**
** }**
This is typical of how all the sounds in my game are played: an event occurs, “ClipPlay” is called with the appropriate integer in parentheses to activate the desired sound, etc…
Can anyone see something I’m doing wrong?