Further to my previous question about showing/hiding guiText, i'm working on a closed caption system where a caption will show when the player comes within range of a sound. I'm using a guiTexture to create the black background, and guiText for the caption String.
Most sounds in the environment are continuous, so they should show a caption all the while the player is in range, then hide the caption if not. However, a couple of sounds are one-shot played by animation events so I want these to show for a period of time specified by me.
I previously got a system working that would hide captions after a specified time, but as described above, this isn't exactly what I want. In trying to amend my code to hide captions if the player moves out of range, I've discovered that now only the one-shot events seem to show up reliably. I believe the reason being is that i'm using one guiText and guiTexture for the one-shot events, and another pair for the general sound effects. Since the general sound effects call the function from Update(), I think they are overriding each other.
Due to this problem, i'm not even sure if my revised IF statement is doing what I want it to now. Also, I'm thinking, rather than creating pairs of guiText/guiTexture for each caption, a better approach is to instantiate them. I know that they will require screen co-ordinates (in my case i'm using x 0.5 and y 0.2), but how would I use this with Object.Instantiate?
Here is my current code:
function showCaption () {
// if this sound source is in range of the player
if(inRange){
// if we are not already showing a caption
if(!textOn){
myCaptionObj.guiText.text = myMessage;
captionBox.guiTexture.enabled = true;
textOn = true;
// if this is a one-shot caption, show it for the specified time
if(!isLooped){
if(myDuration > 0){
yield WaitForSeconds(myDuration);
myCaptionObj.guiText.text = "";
captionBox.guiTexture.enabled = false;
yield WaitForSeconds(myDelay);
textOn = false;
}
}
}
} else {
// if we are out of range, clear the caption and hide the box
myCaptionObj.guiText.text = "";
captionBox.guiTexture.enabled = false;
}
}