- Home /
Radio, multiple sound clips script not working
Hey guys, I have this script, it doesn't seem to recognize the raycast, it plays sounds every time I press Z, not what I want. I only want it to happen when you touch the object with the script applied. I can't seem to figure out the structure
var sound1: AudioClip;
var sound2: AudioClip;
var sound3: AudioClip;
var sound4: AudioClip;
private var radio = "off";
function Update(){
if(Input.GetKeyDown("z")){
{
var hit : RaycastHit;
if (Physics.Raycast(transform.position,
transform.forward, hit, 0.2F))
{
}
if(radio == "off"){
radio = "sound1";
}
else if(radio == "sound1"){
radio = "sound2";
}
else if(radio == "sound2"){
radio = "sound3";
}
else if(radio == "sound3"){
radio = "sound4";
}
else if(radio == "sound4"){
radio = "sound1";
}
if(radio == "sound1"){
Debug.Log("sound is playing");
audio.clip = sound1;
audio.Play();
}
else if(radio == "sound2"){
audio.clip = sound2;
audio.Play();
}
else if(radio == "sound3"){
audio.clip = sound3;
audio.Play();
}
else if(radio == "sound4"){
audio.clip = sound4;
audio.Play();
}
}
}
}
Answer by POLYGAMe · Feb 18, 2014 at 09:48 AM
It could just be the odd formatting, as not all the code is displaying properly as code here (make sure you hilight it all before hitting that binary button) but it looks as though you're doing nothing inside the if raycast hit statement. You're opening and closing brackets and the code will skip straight past. You need to put the other if statements within that one.
Thanks for the input, I'm terrible at writing scripts every time I rearrange the brackets i get error about them, nooo idea where they go lol :(
I find formatting like this helps me keep track of brackets:
if (b$$anonymous$$yBool)
{
//do stuff
}
Remember to accept answers and vote up anything that's helpful :)
I am lost sorry. I'm not sure if the script even works, I found it on here elsewhere then I tried to add raycast myself.. its all beyond me. The sounds still play anytime I press Z, ins$$anonymous$$d of only when I touch object. $$anonymous$$a brainnnnnn hurts now
I suggest researching raycasts and collisions in the docs, maybe even start with some simpler tutorials to get your head around basic scripting.
is a short ray, too. Is your obstacle right in front of the raycasting object?