- Home /
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");
}
Answer by Danilo Nishimura · Apr 14, 2011 at 10:13 PM
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"); } }
This'd be perfect... Better than $$anonymous$$e. 1 problem, display$$anonymous$$essage 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!
You have to set display$$anonymous$$essage = true on the OnTriggerEnter method. I forgot that =/
Can i use this as a boo script or should i try n keep all my scripts in java? thanks for the help guys
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.
Answer by Christopher Travis · Apr 15, 2011 at 12:38 AM
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
$$anonymous$$y bad with the bracket, however display$$anonymous$$essage is actually a String you're to initialize before you actually run the function. And what's wrong now? Does it not work?
Answer by Justin Warner · Apr 14, 2011 at 10:15 PM
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.
thanks for that conversion. ive copied over the code, but it is co$$anonymous$$g 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 ins$$anonymous$$d of semicolon?
ive posted sumthin i hope u can help with as an answer below so its easier to see
Your answer
Follow this Question
Related Questions
HELP with scripting where GUI is involved 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Sliding door animation question. 3 Answers
How to trigger animations in a script using character controller ? 0 Answers
GUIText after animation 0 Answers