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 MickM · Aug 25, 2012 at 09:29 AM · guitimer

Timer issue - bool value not setting timer to false?

Gday all,

I have got a problem with my timer - I want it to keep track of the time it takes from start to finish. I have got a good timer code working thanks to answers from other questions here but I have run into a hurdle trying to stop it.

I have made a maze game that creates levels from scripts rather than constructing it by manually placing the walls down. Initially I just have a Terrain and an empty Game Object called Startup with scripts attached. The attached scripts are Maze Load (Which loads the maze from an array and places the player at start location, exit trigger at the exit and walls etc down according to the characters in the array)

The Exit prefab has a script attached which ends the maze, updates the current level and returns to the menu. The player prefab has the movement script attached.

What I want to do is:

When the maze loads, timer does nothing (Working)

When the player moves, the timer starts (Working but not best practice - using public static variable - Sets a boolean value to true which will commence countdown - see code below)

When the player reaches the end, stop the timer (Not working! Sets the above bool value back to false however the gui timer keeps going!)

Code below but questions:

  1. How do I stop the GUI timer from updating? I know the value is being set to false ('print()' hints in code) but the timer keeps going?

  2. What is a better way to do it rather than use static var? I know you can use the drag and drop method to access objects but as the player and exit is being Instantiated, I cant do this? I have also tried the gameobject find but doesnt work?

Move script (attached to player - note - notMoved var waits until input is recieved then if it is the first input, sets the playerTime variable to true - starts the timer)

 function FixedUpdate () {
     var notMoved = true;
     var moveHoriz = Input.GetAxis ("Horizontal")*speed;
     var moveVert = Input.GetAxis ("Vertical")*speed;
     if (notMoved){
         if ((moveHoriz != 0) || (moveVert != 0)){
             Timer.playerTime = true; //Set player moving to true
             }
     }
     
     rigidbody.velocity = Vector3 (moveHoriz, 0, moveVert);
 }

 

End script (attached to Maze Exit prefab - Note - sets playerTime to FALSE - This should stop the timer but doesnt?)

 function OnCollisionEnter(){
     var currentLevel = Application.loadedLevel;
     var levelCleared = (PlayerPrefs.GetInt("savedLevel"));
     Timer.playerTime = false;
     print ("FALSE!");
 //    PlayerPrefs.SetInt("bestTime"[currentLevel], ?time? ); -- This is to implement (workaround)
     
     yield WaitForSeconds (0.5);
     if (levelCleared <= currentLevel){
         PlayerPrefs.SetInt("savedLevel",(currentLevel + 1));
         PlayerPrefs.Save();
          }
     Screen.lockCursor = false;
     Application.LoadLevel (0);
 }

Timer script - attached to 'Startup' game object (Complete script)

 private var startTime;
 var textTime : String; //added this member variable here so we can access it through other scripts
 public static var playerTime = false;
 
 function Awake(){
     prepStart();
 }
 
 function prepStart(){
     while (!playerTime){
     yield;
     }
     startTime = Time.time;
 }
 
 function OnGUI () {
 
     if (playerTime){
         var guiTime = Time.time - startTime;
         var minutes : int = guiTime / 60;
            var seconds : int = guiTime % 60;
         var fraction : int = (guiTime * 100) % 100;
         textTime = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction); 
         }
     if ((!playerTime)){
         print ("here 1");
         }
    GUI.Label (Rect (400, 25, 100, 30), textTime); //changed variable name to textTime -->text is not a good variable name since it has other use already
 }



Assistance is very much appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Aug 25, 2012 at 09:36 AM

access the var from the other script:

http://answers.unity3d.com/questions/246211/Having-scripts-interact.html

I would instead use:

 var timer:Timer;
 function Start(){
 timer =GameObject.Find("objectName").GetComponent(Timer);
 }
 function Update(){
 if ((moveHoriz != 0) || (moveVert != 0)){
          timer.timeElapsedMoving +=Time.deltaTime true; 
 }
 }

Now when your player is moving, the var timeElapsedMoving (float) is added deltaTime. So if you spend 2 s moving....well it is 2.

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

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

Timer progress bar 0 Answers

Countdown Timer 1 Answer

GUI elements vanish when publishing 1 Answer

scaling a gui texture 2 Answers

How Would I Make A GUI Label Fade After A Certain Amount Of Time? 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