- Home /
I need a function so that when and event is trigger a message is displayed on the screen for a set duration.
something like this
function message(msg, duration) { // code i need to display message(msg) for duration (duration) seconds }
then when something happens i want to do something like this message("you picked up the box" , 2);
you wanna do this using UnityGUI? or ... what gui system you have?
Answer by DeveshPandey · Sep 17, 2013 at 10:53 AM
you can try this:
var showMsg = false;
var msgText:string = "";
function OnGUI()
{
if(showMsg)
{
GUI.Label(Rect(100,100,200,100),msgText);
}
}
function ShowMessage(msg:string,duration:float)
{
showMsg = true;
msgText = msg;
yield WaitForSeconds (duration);
showMsg = false;
}
// you can call ShowMessage() from anywhere you want, I have called on Start()
function Start()
{
ShowMessage("This is your message",2);
}
That all makes sense thanks. One further silly question. Where do I put/attach this script ?
create a empty gameobject in your hierarchy and put this script on it.
yes i already done that , then i added this to a falling object:
function OnCollisionEnter(){
Show$$anonymous$$essage("hit",2);
}
but get : $$anonymous$$ Identifier Show$$anonymous$$essage
ps. Sorry to be a pain.
its working fine here... have put
function OnCollisionEnter(){
Show$$anonymous$$essage("hit",2); }
in the same script??? if not then working code will be -
var show$$anonymous$$sg = false;
var msgText = "";
function OnGUI()
{
if(show$$anonymous$$sg)
{
GUI.Label(Rect(100,100,200,100),msgText);
}
}
function Show$$anonymous$$essage(msg,duration)
{
show$$anonymous$$sg = true;
msgText = msg;
yield WaitForSeconds (duration);
show$$anonymous$$sg = false;
}
// you can call Show$$anonymous$$essage() from anywhere you want, I have called on Start()
function Start()
{
Show$$anonymous$$essage("This is your message",2);
}
function OnCollisionEnter(){
Show$$anonymous$$essage("hit",2);
}
you can paste my code where you have written your function OnCollisionEnter(), you don't need to find gameobject.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
In Game player message - GUIText or something else? 1 Answer
Message Box Question(please read) 2 Answers
Big text field GUI. 1 Answer
Displaying varying text 1 Answer