- Home /
print on screen
okay so i have an hit box (isTgigger) i want to make it so that when my player hits the box a GUI text appears on screen, displaying some information, the text is removed upon the player ceasing contact with the hit box. i have a code wich i don't know if will work or not.
function OnTriggerEnter (other : Collider) { #inser code for printing info on screen here# }
can some1 help me?
Answer by AlucardJay · Sep 30, 2012 at 02:57 PM
Keep track of if something is within the trigger with a boolean. Then if that boolean is true, display the GUI :
var withinTrigger : boolean = false;
function OnTriggerEnter (other : Collider)
{
withinTrigger = true;
}
function OnTriggerExit (other : Collider)
{
withinTrigger = false;
}
function OnGUI()
{
if ( withinTrigger )
{
GUI.Box( Rect( (Screen.width * 0.5) - 100, 10, 200, 25 ), "Within the Trigger !" );
}
}
Ahh yeah I misread, I though he was using the guiText component ... its a much better idea to do it this way, although not sure why you used GUI.BOX over GUI.Label
Force of habit, I only use Unity's GUI for debugging / displaying stats while testing, and always use Box for that! I think it's for the contrast and the centering of the text. I have written and use my own GUI for my projects.
tnx dude. i'm using this as a tutorial in my game. (when approaching the first gap it's gonna display: "press Space to jump" and u'r script was just what i needed
the code formatting has not included the first line, so if you just copied then you are missing the first line. I have just fixed the code block (the first line was always there, perhaps read the code?).
Are you familiar with trigger? http://www.unity3dstudent.com/2010/07/beginner-b13-trigger-collision-detection/
If you are just starting out, then the whole series might be helpful to watch :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials
A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
Your answer
Follow this Question
Related Questions
GUI text font change 3 Answers
Limit on GUI Components? 0 Answers
Using "fonts" that are actually images for a multi-outline effect. 2 Answers
Dialouge text. 1 Answer
Unity GUI text displaying as noise 1 Answer