- Home /
Feast or Famine with GuiText
I can't believe I'm asking this. Go ahead, yell at me, tell me to search. I deserve it, because this is a hella simple question that for some reason I haven't quite found the answer to.
You see an item. You pick it up. When you pick it up, I'd just like a brief little text to appear that says what you picked up, then disappear shortly after. "Congrats, you just found dirty underwear!" or something like that.
So at first I couldn't get anything to appear at all. After a little bit of tweaking, I got the text to appear, but it's there right from the start. So now the screen is littered with text boxes. Sorry, really. Here's the code. Thanks for helping, and God bless.
var pickuphardtacktext = true;
var textArea = new Rect(0,0,Screen.width, Screen.height);
var pickuphardtack : GUIText;
function Update () {
}
function OnTriggerEnter (col : Collider) {
pickuphardtacktext = true;
if (col.gameObject.tag == "Player") {
Inventory.inventoryArray[1]++;
Destroy(this.gameObject);
}
}
function OnGUI()
{
if(pickuphardtacktext)
{
GUI.Label(textArea, "You found a Hard Tack Cracker!");
}
}
Also, yep, I see that I have both a GuiText object appear, as well as text right in the script. Not wanting both, I just throwing stuff at the problem to see what would stick.
This code won't work, because the object is destroyed right after having been picked up. When the object disappears, so does every script attached to it, including this one.
Anoother issue is that your textArea Rect is set to the lower right hand corner of the screen, so nothing is printed on screen even if OnGUI ran with pickuphardtacktext
set to true.
I had considered that, but had hoped that maybe if I placed the text item early enough (before the object was destroyed) that I could still achieve the desired result. If that's not possible, I wonder if I could call another script attached to an empty game object with the text attached to it?
Answer by MauMaGau · Sep 24, 2013 at 09:36 PM
I've tackled something similar by having a GUI script (attached to an empty game object with a load of other 'manager' scripts) with the following code in it's OnGui method:
if(!String.IsNullOrEmpty(GUIMessageCenter)){
GUI.Box(Rect(Screen.width / 2 - 200, Screen.height / 2, 400, 25), GUIMessageCenter);
}
GUIMessageCenter being a public variable that I set from other scripts via a coroutine (to allow the message to remain for x seconds):
GUIScript.GUIIMessageCenter = "Matter hose not connected";
yield WaitForSeconds(2);
GUIMessageCenter = "";
This is pretty hacky, and could be much better written, but hopefully it gives you some ideas
Your answer
Follow this Question
Related Questions
How to get print to transfer to a GUI Text? 1 Answer
How do I get my GUI text to open on awake? 1 Answer
GUI Text Score counter 3 Answers
Gui Text Script 4 Answers
Intro GUI Text Script... 3 Answers