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 josak46 · Jul 17, 2014 at 04:44 PM · gametimetimerbest

Best time and saving best time

Hi!

I've made a timer in my game. This is the script of it. But I want to save best time. When a player reaches currentScore 20 I want timer to stop and then save the time if it is the best and I want it to be in GUI Box.

 var timer : float = 0.00;
 function Update () 
 {
     timer += Time.deltaTime;
 }
  
 function OnGUI ()
 {
     GUI.Box(new Rect(10, 10, 70, 20), "" + timer.ToString (""));
  
 }

I was trying to do something but it doesn't work. Please understand I'm a beginner. I tried to do something logical. But it says that it is expecting ), found = and unexpected token: 20.

 static var currentScore : int = 0;
 var timer : float = 0.00;
 var bestTime = 0;
  
 function Start () {
  
 }
 function Update () {
  if (currentScore = 20) {
    if (bestTime > timer){
   bestTime = timer;
   PlayerPrefs.Save();
   }
  }
 }
  
 function OnGUI ()
 {
     GUI.Box(new Rect(90, 10, 70, 20), "" + timer) ;
  
 }
  

Please help. Thank you.

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
2
Best Answer

Answer by robertbu · Jul 17, 2014 at 04:52 PM

The syntax error is because comparisons use double equals. So line 9 should be:

  if (currentScore == 20) {

For future posts, please include a copy of the error message from the Console. It gives us the line number and the stack trace.

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 josak46 · Jul 17, 2014 at 05:19 PM 0
Share

Thank you, now it works :). But now I have another problem best time doesn't save.

avatar image
0

Answer by yagizpoyraz · Jul 17, 2014 at 05:15 PM

For save:

 PlayerPrefs.SetFloat("bestTimer", bestTimer);
 PlayerPrefs.Save();

For read:

 var bestTimer = PlayerPrefs.GetFloat("bestTimer")

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 josak46 · Jul 17, 2014 at 05:32 PM 0
Share

You mean like this? Because save still doesn't work. :/

 static var currentScore : int = 0;
 var timer : float = 0.00;
 var bestTime = 0;
 var bestTimer = GUI.Box;
  
 function Start () {
  
 }
 function Update () {
  if (currentScore == 20) {
    if (bestTime > timer){
   bestTime = timer;
   PlayerPrefs.Save();
   }
  }
 }
  
 function OnGUI ()
 {
     GUI.Box(new Rect(90, 10, 70, 20), "" + timer) ;
  
 }
avatar image yagizpoyraz · Jul 17, 2014 at 06:16 PM 0
Share

For save:

 PlayerPrefs.SetFloat("bestTimer", bestTimer);
 PlayerPrefs.Save();

For read:

 var bestTimer = PlayerPrefs.GetFloat("bestTimer");


avatar image josak46 · Jul 17, 2014 at 08:23 PM 0
Share

Sorry. I don't understand. Where do I put this?

avatar image NickP_2 josak46 · Feb 16, 2016 at 09:43 AM 0
Share

take a look at **this** video, it explains a bit UI stuff too. OnGUI is old and slow, don't touch it for in-game purposes.

avatar image
0

Answer by 265lutab · Feb 16, 2016 at 01:14 PM

like this

 static var currentScore : int = 0;
  var timer : float = 0.00;
  var bestTime = 0;
  var bestTimer = GUI.Box;
   
  function Start () {
   
  }
  function Update () {
   if (currentScore == 20) {
     if (bestTime > timer){
    bestTime = timer;
     PlayerPrefs.SetFloat("bestTimer", bestTimer);
  PlayerPrefs.Save();
    }
   }
  }
   
  function OnGUI ()
  {
      GUI.Box(new Rect(90, 10, 70, 20), "" + timer) ;
   
  }
 
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set the best time and save it? 1 Answer

How to make a timer read to the .001 of a second 2 Answers

Timer Countdown fix. 2 Answers

timer that can be reset 1 Answer

How to work a real life timer? 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