- Home /
Instantiating guiText / guiTexture
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;
}
}
Answer by Eric5h5 · Apr 16, 2010 at 06:41 PM
You instantiate them the same way as anything else:
var textObject : GUIText;
Instantiate(textObject); // or Instantiate(textObject, Vector3(.5, .2, 0.0), Quaternion.identity);
Thanks Eric, I wasn't sure about the last parameter. Instantiating works fine now, but I need to get my head around destroying the object. I'm finding it more complicated since the above script is not on the caption guiText itself. I may just go ahead with my older script for the time being since i'm on a deadline.
@straydogstrut: To destroy an instantiated object, do var obj = Instantiate(textObject);
and then Destroy(obj);
when appropriate.
Your answer
Follow this Question
Related Questions
GUITextures not lining up correctly 2 Answers
Multiplying GUIs 0 Answers
When I built my game in xcode all the gui textures/texts look different 1 Answer
Gui , Message , Text, Touch and Question ? 2 Answers
Dynamically instantiated game objects not showing in main camera (with background camera) 3 Answers