- Home /
Playing multiple sounds with multiple subtitles
Hi,
I recently was working on getting an audio array to play, so when the user presses E on the object, it will loop through three different sounds.
I am now stuck again and have been trying to get three GUIText (Subtitles) to enable with the audio on each loop. So basically, I want three GUITexts to have their own looper and for the Alpha to become visable when the person is speaking, then if they press E again, the second text will appear, and so on.
Original Code
public var soundClip : AudioClip[]; //to be assigned in inspector
private var looper : int = 0;
function OnTriggerStay (col:Collider){
if(Input.GetKey("e")) {
if(!audio.isPlaying)
{
audio.clip = soundClip[looper];
audio.Play();
looper++;
looper = looper%soundClip.Length;
}
}
}
Stuck Code
public var soundClip : AudioClip[]; //to be assigned in inspector
public var gui : GUIText[];
private var looper : int = 0;
private var looperGUI : int = 0;
function OnTriggerStay (col:Collider){
if(Input.GetKey("e")) {
if(!audio.isPlaying)
{
audio.clip = soundClip[looper];
audio.Play();
looper++;
looperGUI++;
looper = looper%soundClip.Length;
}
}
}
So, if anyone has the time to help me, it would be appriceated as this is really bugging me. I don't mind using alpha changes or enabling/disabling the GUI text, you tell me what is best.
Answer by Nerevar · Jun 28, 2014 at 02:32 AM
Hello again :p
This may work:
public var soundClip : AudioClip[]; //to be assigned in inspector
public var gui : GUIText[];
private var looper : int = 0;
private var looperGUI : int = 0;
function OnTriggerStay (col:Collider){
if(Input.GetKey("e")) {
if(!audio.isPlaying)
{
HideSubs();
audio.clip = soundClip[looper];
gui[looperGUI].enabled = true;
audio.Play();
looper++;
looperGUI++;
looperGUI = looperGUI%gui.Length;
looper = looper%soundClip.Length;
}
}
}
function Update(){
if(!audio.isPlaying)
{
HideSubs();
}
}
function HideSubs(){
for(var i = 0 ; i< gui.Length; i++)
{
gui[i].enabled = false;
}
}
Tell me if you have any unwanted behaviours, I did not fully tested it so some edition might be needed.
see you
Hey again! Hahaha, thanks for responding so fast as well! I just tried doing your new script and there was no code errors, however it didn't work.
So, I went up to the object and pressed E, and it didn't even play a sound, nor bring up the GUI text that I assigned in the inspector. Do you think I need to give the GUI text a length or something? Because I'm not too sure how to do that.
Let me know what you think could be the problem and then I will change it. :)
$$anonymous$$mh, Well the audio should be playing I did not touched this part. You just need to assign the clips and the GUITexts from the inspector.
Do you have any errors on Play?
You know what?! I don't know what it was, I think maybe the fact I had three elements of GUI and two of them were none, I changed it to 1 and now it works perfectly!! You are a genius, I was just being stupid hahaha! You seem really good in JavaScript, is there anyway I can contact you directly for advise? I'm eager to learn more about JS. :)
Your answer
Follow this Question
Related Questions
Gui Button Solid 2 Answers
GUI Text from script Java 1 Answer
gameObject does not function properly when used as an asset 0 Answers
Limit on GUI Components? 0 Answers
Detect Text in GUI; Print 1 Answer