x


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;
    }
}
more ▼

asked Apr 16 '10 at 05:28 PM

straydogstrut gravatar image

straydogstrut
1.2k 29 38 60

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You instantiate them the same way as anything else:

var textObject : GUIText;

Instantiate(textObject);
// or Instantiate(textObject, Vector3(.5, .2, 0.0), Quaternion.identity);
more ▼

answered Apr 16 '10 at 06:41 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

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.

Apr 16 '10 at 11:07 PM straydogstrut

@straydogstrut: To destroy an instantiated object, do var obj = Instantiate(textObject); and then Destroy(obj); when appropriate.

Apr 17 '10 at 12:36 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1726
x495
x319

asked: Apr 16 '10 at 05:28 PM

Seen: 5197 times

Last Updated: Apr 16 '10 at 05:28 PM