My Script is not working properly. I have multiple prefabs called SoundBoxes, if my character collides with them I would like to either play a random sound from a folder or a selected sound.(Just play the sound selected in the AudioSource) My script does not seem to work properly. It never plays the sound I want and sometimes executes both if and else… I noticed that the PlaySound function seems to rune twice although it should not.
I would be really happy if someone could tell me how to make this look like script(with the blue if and var)
Thank you,
Pit
Code:
#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;
}