Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by Karela · Dec 06, 2010 at 09:14 PM · messagetimer-script

How to set On Screen Message w/ timer

I have a puzzle game where the player wanders this endless puzzle, and after a certain period of time, I want a message to pop up saying there is no end, and then the game ends. I am new to Unity and would like some assistance in setting this up, so any tutorials/scripts/hints would be very helpful, thanks!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Dec 06, 2010 at 10:17 PM

Just use Invoke:

var timeToWait = 120.0;

function Start () { Invoke("GameOver", timeToWait); }

function GameOver () { // do whatever here }

Comment
Add comment · Show 1 · 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 Statement · Dec 06, 2010 at 10:19 PM 0
Share
  • short and clean.

avatar image
0

Answer by Jesus_Freak · Dec 06, 2010 at 09:32 PM

here's what i use to post time... you could use that to show a timer. right now, the time is always showing.... and you might want to change the hours, seconds, and minutes yourself, and maybe change the values in the script yourself. but i'll provide the message thing.

var showGUI = false; var Timer : int = 0; var seconds : int = 0; var CTRL : int = -100; var minutes : int = 0; var hours : int = 0; var CTimer : int = 0; function Update() { CTimer++; Timer--; if(Timer == CTRL) { seconds --; Timer = 0; } if(seconds == -60) { minutes --; seconds = 0; } if(minutes == -60) { hours --; minutes = 0; } if(hours == -25) { hours = 1; } if(CTimer == -500)//at about 5 seconds, do something. { //print GUI as in turn a showGUI boolean to true. showGUI = true; } }

 //GUI function to print the time the player spent playing
 function OnGUI()
 {
  if(showGUI)
  {
   GUILayout.Button("there is no end!");
  }
  GUI.Label(Rect(20,100, 400,30), "Time past: " +hours * -1+":"+minutes * -1+":"+seconds * -1);
 }

Comment
Add comment · Show 9 · 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 Karela · Dec 06, 2010 at 09:36 PM 0
Share

I add the starting time at the bottom I am guessing, correct? Would it automatically count down or would I have to find another script to make it count down?

avatar image Jesus_Freak · Dec 06, 2010 at 09:41 PM 0
Share

oops! sorry... that counts up.... i'll work on a timer countdown... sorry!

avatar image Jesus_Freak · Dec 06, 2010 at 09:57 PM 0
Share

there. that sets the counter to count down and it might display the GUI as "Time past: 0:-2:-27" for example. 2 $$anonymous$$utes and 27 seconds passed. i don't think that's what you want, and i'm not sure if that's how it prints as the gui... i'll try it out. but that's basically good and updated as of now.

avatar image Jesus_Freak · Dec 06, 2010 at 10:01 PM 0
Share

now, time counts up, but it's showing the time through the GUI as counting up, but the values themselves stay the same. and it counts "up" as regular.

avatar image Jesus_Freak · Dec 06, 2010 at 10:01 PM 0
Share

by the way, the values are decreasing over time. believe it or not!

Show more comments
avatar image
0

Answer by Statement · Dec 06, 2010 at 10:04 PM

I think this C# script should do what you're looking for. It waits 60 seconds, then pops up a big button over the screen. Once button is pressed, the application exits.

It only exits if it is a built executable.

using UnityEngine; using System.Collections;

public class ExitTimer : MonoBehaviour { public string message = "There is no end..."; private bool pressedOk = false; private bool showGui = false;

 private IEnumerator Start ( )
 {
     // Wait one minute.
     yield return new WaitForSeconds( 60.0f );

     // Wait for button press.
     yield return StartCoroutine( WaitForOkButton( ) );

     // Quit application (doesn't work in Editor I think).
     Application.Quit( );
 }

 private IEnumerator WaitForOkButton ( )
 {
     // Show gui
     showGui = true;

     // Wait for Gui
     pressedOk = false;
     while ( !pressedOk )
         yield return null;

     // Hide gui
     showGui = false;
 }

 private void OnGUI ( )
 {
     if ( showGui )
     {
         Rect fullScreen = new Rect( 0, 0, Screen.width, Screen.height );
         if ( GUI.Button( fullScreen, message ) )
         {
             pressedOk = true;
         }
     }
 }

}

This example makes use of Co-routines. It allows your logic to pause its execution in place, without actually stalling the rest of the program. I suggest you learn about co-routines as they are really nice when working with scripts that require sequential action like the one you described (any waiting actions, basically).

To use this, just place the script on any game object (that isn't deleted, or the timer stops). You need to match the file name with the class name (unless they changed that since 2.6 era), so you should put this code in a new file called ExitTimer.cs.

Comment
Add comment · Show 1 · 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 Statement · Dec 06, 2010 at 10:05 PM 0
Share

See if you can't make the timer configurable by adding a new public field, so you can reuse your script a bit more. :)

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Sending Messages to Other game Objects? 1 Answer

Triggered on screen hint message. 1 Answer

Parametrize generic event system (DRY it) 1 Answer

Sending Messages with Unet 2 Answers

End Game When Time Is Up 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