• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by mmidd · Sep 17, 2013 at 10:22 AM · guitextmessagemessagebox

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);

Comment

People who like this

0 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Sep 17, 2013 at 10:25 AM 0
Share

you wanna do this using UnityGUI? or ... what gui system you have?

avatar image mmidd · Sep 17, 2013 at 10:32 AM 0
Share

Yes i am using Unity - so i was thinking GUI text

1 Reply

· Add your reply
  • Sort: 
avatar image

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);
 }
Comment
vexe
erdavis

People who like this

2 Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mmidd · Sep 17, 2013 at 11:02 AM 0
Share

That all makes sense thanks. One further silly question. Where do I put/attach this script ?

avatar image DeveshPandey · Sep 17, 2013 at 11:30 AM 1
Share

create a empty gameobject in your hierarchy and put this script on it.

avatar image mmidd · Sep 17, 2013 at 11:33 AM 0
Share

yes i already done that , then i added this to a falling object:

function OnCollisionEnter(){

 ShowMessage("hit",2);
 

}

but get : Unknown Identifier ShowMessage

ps. Sorry to be a pain.

avatar image DeveshPandey · Sep 17, 2013 at 12:01 PM 1
Share

its working fine here... have put

function OnCollisionEnter(){

ShowMessage("hit",2); }

in the same script??? if not then working code will be -

 var showMsg = false;
 var msgText = "";
  
 function OnGUI()
 {
      if(showMsg)
      {
           GUI.Label(Rect(100,100,200,100),msgText);
      }
 }
  
 function ShowMessage(msg,duration)
 {
      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);
 }
 
 function OnCollisionEnter(){
 
     ShowMessage("hit",2);
 }
avatar image DeveshPandey · Sep 17, 2013 at 12:10 PM 1
Share

you can paste my code where you have written your function OnCollisionEnter(), you don't need to find gameobject.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Message Box Question(please read) 2 Answers

In Game player message - GUIText or something else? 1 Answer

Create GUIText from Javascript 3 Answers

Difference between a script in the scene and in instantiated 1 Answer

How To Make Floating Combat Text In new UI System of 4.6 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges