- Home /
Question by
Pitomator · Apr 23, 2013 at 01:22 PM ·
javascripterrorsoundfolder
Sound system not working correctly
I have a prefab called SoundBox that I use multiple times in my scene. it has a script attached to it which plays either a selected sound or a sound from a folder. Sadly my script does not function as intended. It runs the PlaySound function twice, I believe because the debug message is sent twice. Also sometimes it runs both if and else?!
Thank you, Pit
#pragma strict
//
// play a random sound from a folder
//
var isPlaying : boolean = false;
var thisSound : AudioSource;
var myClips : Object[];
var hasPlayed : boolean = false;
var playSelected : boolean;
function Start(){
myClips = Resources.LoadAll("Sounds/Voices",AudioClip);
}
function Update()
{
if (StaticGameData.restart == true)
{ResetObject();}
}
function OnTriggerEnter(cc : Collider){
if(cc.gameObject.tag == "Player" && !hasPlayed){
PlaySound();
}
}
function OnTriggerExit(cc : Collider){
hasPlayed = true;
}
function PlaySound()
{
if(playSelected == false)
{
audio.clip = myClips[StaticGameData.whatSound];
audio.Play();
Debug.Log("has played normal");
if (StaticGameData.whatSound+1 == myClips.Length)
{
StaticGameData.whatSound = 0;
}
else
{
StaticGameData.whatSound = StaticGameData.whatSound +1;
}
}
else
{
audio.Play();
Debug.Log("has played selected");
}
}
function ResetObject()
{
hasPlayed = false;
StaticGameData.restart = false;
}
Comment
Your answer
Follow this Question
Related Questions
'Play' is not a member of 'Object' 1 Answer
Implicit Downcast warning. 1 Answer
Enemy AI Error 1 Answer