x


How do i make a text box appear on screen after a chest has been opened?

Hi, ive made a chest animation triggered when a player reaches the chest that it opens up, i now want to make it so once this animation is triggered a text box will appear saying what you received out of the chest.

How do i go about doing this?

This is the script i already have in place incase it is needed.

  function OnTriggerEnter (Player : Collider) 
  {  
    animation.Play("Take 001");
  }
more ▼

asked Apr 14 '11 at 08:40 PM

Christopher Travis gravatar image

Christopher Travis
17 6 7 9

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

3 answers: sort voted first

Hi,

If you want to draw something on GUI, you can use the OnGUI method for GUI drawing.

see if this sample helps (someone help to port it to JS for him?) :

bool displayMessage = false;

void OnTriggerEnter ( Collider Player )
{
    animation.Play("Take 001");
}

void OnGUI ( )
{
    if ( displayMessage )
    {
        GUI.Label(new Rect(Screen.width * 0.5f - 50f, Screen.height * 0.5f - 10f, 100f, 20f), "Testing");
    }
}
more ▼

answered Apr 14 '11 at 10:13 PM

Danilo Nishimura gravatar image

Danilo Nishimura
13 2 2 7

This'd be perfect... Better than mine. 1 problem, displayMessage never becomes true... So in the OnTriggerEnter, make it true, then put in the yield, and make it false again after a certain amount of time. Nice Danilo!

Apr 14 '11 at 10:16 PM Justin Warner

You have to set displayMessage = true on the OnTriggerEnter method. I forgot that =/

Apr 14 '11 at 10:18 PM Danilo Nishimura

Can i use this as a boo script or should i try n keep all my scripts in java? thanks for the help guys

Apr 14 '11 at 10:29 PM Christopher Travis

I think the result is the same as it's compiled to an intermediate language before the final compilation. I recommend you to write all your scripts in the same language to maintain a coding pattern and you won't get confused about which language to use when writing new files. Even if you get a sample code from somewhere, I recommend to rewrite it.

Apr 15 '11 at 02:04 PM Danilo Nishimura
(comments are locked)
10|3000 characters needed characters left

Make this a seperate script on the same object...

function OnGUI () {
    GUI.Label (Rect (10, 10, 100, 20), "You picked up a pick ax!");
}

Then....

  function OnTriggerEnter (Player : Collider) 
  {  
    animation.Play("Take 001");
    GetComponent(SHOWMESSAGE).enable = true;
    yield WaitForSeconds(3);
    GetComponent(SHOWMESSAGE).enable = false;

  }

Check out how to use GetComponent: http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html

Good luck!

TO _ JAVA

displayMessage = false;

function OnTriggerEnter (Player : Collider) {
{
    animation.Play("Take 001");
    displayMessage = true;
    yield WaitForSeconds(3);
    displayMessage = false;
}

function OnGUI ( )
{
    if ( displayMessage )
    {
        GUI.Label(new Rect(Screen.width /2, Screen.height / 2, 200, 200), "Testing");
    }
}

Ignore mine, use his, but this can work I think... Not the best when not on my computer.

more ▼

answered Apr 14 '11 at 10:15 PM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

thanks for that conversion. ive copied over the code, but it is coming up with this problem,

Assets/NewBehaviourScript.js(5,43): BCE0044: expecting :, found ';'.

i dont know how to solve this one, should one of the lines have a colon instead of semicolon?

Apr 14 '11 at 11:32 PM Christopher Travis

ive posted sumthin i hope u can help with as an answer below so its easier to see

Apr 15 '11 at 12:31 AM Christopher Travis
(comments are locked)
10|3000 characters needed characters left

I had the problem i mentioned in the comments above with the code you supplied, so i made a few alterations so there were no errors and i could get in play mode, but now the text just appears from the start and doesnt dissapear rather than appearing when the animation is triggered.

heres the new code ive had to use so it said there were no errors. changes i had to make were,

function OnTriggerEnter (Player : Collider) {
                to
function OnTriggerEnter (Player : Collider) 

it didnt like the extra { at the end because it wasnt attached to anything, when i tried just adding another one of them at the end it caused more errors. ALSO

if ( displayMessage )
       to
if ( "displayMessage" ) 

im guessing these changes are the reason it doesnt appear at the right time but it was the only way to remove the errors. Is there any help you can give to correct this so it appears at the right time when the animation is triggered?

Thanks

more ▼

answered Apr 15 '11 at 12:38 AM

Christopher Travis gravatar image

Christopher Travis
17 6 7 9

My bad with the bracket, however displayMessage is actually a String you're to initialize before you actually run the function. And what's wrong now? Does it not work?

Apr 15 '11 at 01:35 AM Justin Warner
(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:

x5070
x3774
x3680
x982
x313

asked: Apr 14 '11 at 08:40 PM

Seen: 2511 times

Last Updated: Apr 14 '11 at 10:12 PM