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
0
Question by MatthewSmoothy · Jul 04, 2012 at 05:36 AM · timerguitextongui

Help with timer guitext

Hey im using two scripts, startRace & endRace, which starts at 60 seconds counting down when you trigger it and stops when you hit the other trigger. Can i just add some coding into these codes which make it a guitext and displays on the screen. Thank you.

startRace :

 using UnityEngine;
 using System.Collections;
 
 public class startRace : MonoBehaviour {
  
  public bool raceStarted = false;
  public float raceTimer = 60.0f;
  public bool raceOver = false;
 
 
  // Use this for initialization
  void Start () {
  
  }
  
  // Update is called once per frame
  void Update () {
  
  //Starts timer when race starts
  if (raceStarted)
  {
  raceTimer -= Time.deltaTime;
  }
  if (raceTimer <= 0.0f)
  {
  raceOver = true;
  raceStarted = false;
  Application.LoadLevel(0);
  }
  
  }
  
  void OnTriggerEnter(Collider raceTrig)
  
  {
  
  //Starts race
    if(raceTrig.gameObject.tag == "Player")
  {
  raceStarted = true; 
  }
  }
  
 }

endRace:

 using UnityEngine;
 using System.Collections;
 
 public class endRace : MonoBehaviour {
  
  private startRace race;
  
  // Use this for initialization
  void Start () {
  
  race = FindObjectOfType(typeof(startRace)) as startRace;
  
  }
  
  // Update is called once per frame
  void Update () {
  
  }
  
  void OnTriggerEnter(Collider raceTrig)
  
  {
  
  //Stops timer and ends race
  
  if(raceTrig.gameObject.tag == "Player")
  {
  race.raceStarted = false; 
  }
  
  
  }
 }
Comment
Add comment · Show 4
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 Tim-Michels · Jul 04, 2012 at 06:45 AM 0
Share

Do you want to use 3D-text, or OnGUI?

avatar image MatthewSmoothy · Jul 04, 2012 at 06:48 AM 0
Share

I want to use OnGUI

avatar image spinaljack · Jul 04, 2012 at 10:34 AM 0
Share

please use the code tags

avatar image MatthewSmoothy · Jul 04, 2012 at 10:39 AM 0
Share

sorry i will next time, do you have any idea about the new question i asked below, thanks :)

2 Replies

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

Answer by Tim-Michels · Jul 04, 2012 at 06:52 AM

Well here's a basic display of text in OnGUI:

 void OnGUI()
     {
         GUI.Label(new Rect(x, y, width, height), raceTimer.ToString());
     }

You should alter the Rect to position it to where you want. You should also take a look at

http://docs.unity3d.com/Documentation/ScriptReference/GUIStyle.html

to understand how to alter the style, size, etc. of your text.

This should help you on your way ;) cheers

Comment
Add comment · Show 4 · 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 MatthewSmoothy · Jul 04, 2012 at 07:18 AM 0
Share

Thanks very much worked perfectly.

avatar image flamy · Jul 04, 2012 at 07:22 AM 0
Share

well to add to this answer, here is a function that would work even if the time is more than 60 seconds!!! and display it in standard clock format.

 public string GetDisplayTime(float time)
 {
  
  int $$anonymous$$ute =  (int) $$anonymous$$athf.Abs(time/60); 
  int seconds = ((int) time)%60;
  
  return $$anonymous$$ute.ToString("00")+":"+seconds.ToString("00");
 }

 void OnGUI()
     {
         GUI.Label(new Rect(x, y, width, height), GetDisplayTime((float)raceTimer));
     }
avatar image MatthewSmoothy · Jul 04, 2012 at 07:39 AM 0
Share

The first code is easier for me and it works in game perfectly and just what i want it to do, but thank you anyway. But now i need to know what i have to add to either of those scripts so that once it hits that last trigger and the timer stops. I would like the game to freeze once the end trigger has been set, and for a new guitext to appear in the middle of the screen saying "You Win!"

avatar image MatthewSmoothy · Jul 04, 2012 at 09:11 AM 0
Share

I want to add a guitext, to this script so that when my player hits the collider, it will freeze the game and text will appear saying "you win" for 5 seconds then go back to my main menu. The player is already hitting the collider and the timer is stopping but the player is still able to move.

using UnityEngine; using System.Collections;

public class endRace : $$anonymous$$onoBehaviour {

private startRace race;

// Use this for initialization void Start () {

race = FindObjectOfType(typeof(startRace)) as startRace;

}

// Update is called once per frame void Update () {

}

void OnTriggerEnter(Collider raceTrig)

{

//Stops timer and ends race if(raceTrig.gameObject.tag == "Player") { race.raceStarted = false; }

}

}

avatar image
0

Answer by MatthewSmoothy · Jul 04, 2012 at 12:03 PM

I want to add a guitext, to this script so that when my player hits the collider, it will freeze the game and text will appear saying "you win" for 5 seconds then go back to my main menu. The player is already hitting the collider and the timer is stopping but the player is still able to move.

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 HolBol · Jul 04, 2012 at 12:11 PM 0
Share

Do not post comments or edits to your initial question in the answer section! Use this space for actual answers to the original question and nothing else. Use the [add new comment] button that is underneath each answer or question.

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

8 People are following this question.

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

Related Questions

Centering GUIText 1 Answer

GUIText not working properly with a timer. 1 Answer

gameTimer needs to be based on an event, not onGUI 2 Answers

how to make information slideshow? 0 Answers

Placing a chat bubble above a character's head in a 3d environment 2 Answers


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