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 AndroidWG · Jul 30, 2014 at 01:05 AM · playerprefsinfinite runnerrecord

Best Distance (Record) System on Infinite Runner game

I'm Brazillian. Dont look for errors!

I'm making an Infinite Runner game on Unity, and I want to make an Record system, measured by the distance. Here's the script I made based on a YouTube tutorial:

 #pragma strict
 
 var StartPOS: Vector3;
 var Record: float;
 var Distance: float;
 var CustomSkin: GUISkin;
 var pause : boolean = false;
 var pauseGUI : GUIText;
 pauseGUI.enabled = false;
 
 function Start () {
     StartPOS = transform.position;
     Record = PlayerPrefs.GetInt("Reccord");
 }    
 
 function Update(){
 
 }
 
 function OnGUI () {
     Distance = transform.lossyScale.magnitude;
     Distance = Vector3.Distance(transform.position,StartPOS);
     GUI.skin = CustomSkin;
     GUI.Label(new Rect(Screen.width/10,Screen.height/10,Screen.width/1,Screen.width/10),"Distance (m): "+ Distance.ToString("F1"));
     
     GUI.skin = CustomSkin;
     GUI.Label(new Rect(Screen.width/10,Screen.height/5,Screen.width/1,Screen.width/10),"Best (m): "+ Record.ToString("F1"));
     
     if (Record<Distance){
         PlayerPrefs.GetInt("Reccord",Mathf.Floor (Distance));
     }
 }
 
 function OnMouseOver () {
 
     guiText.material.color = Color.yellow;
 }
 
 function OnMouseExit () {
 
     guiText.material.color = Color.white;
 }
 
 function OnMouseDown () {
     Time.timeScale = 1.0;
     pauseGUI.enabled = false;
     Destroy(this);
     Application.LoadLevel ("InfiniteUnity");
 }

On the GUI it appears like this:

alt text

And in the editor it appears like this:

alt text

This means that the game is not measuring the record distance, for some reason. Can someone help me? Thanks a lot!

screenshot_34.png (6.7 kB)
screenshot_35.png (9.7 kB)
Comment
Add comment · Show 3
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 robertbu · Jul 30, 2014 at 01:16 AM 0
Share

Not sure of your exact problem, but I see two issues. The value assigned in line 21 is completely overwritten in line 22, so line 21 is useless. Distance is an int, so on line 22, if the distance calculation is less than 1.0, it will evaluate to the int 0. Note the use of upper case 'Distance' is confusing, especially since you are using Vector3.Distance() as well.. By convention, variables should start with a lower case letter.

avatar image AndroidWG · Jul 30, 2014 at 11:48 PM 0
Share

robertbu, I know, but the Distance is being measured well. The problem is the Reocrd lines. Correct me if I'm wrong, please

avatar image Maco21 · Jul 07, 2015 at 01:54 PM 0
Share

AndroidWG can i have your email, i have some questions :))

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TruffelsAndOranges · Jul 07, 2015 at 02:00 PM

PlayerPrefs.GetInt("Reccord",Mathf.Floor (Distance)); ... pretty sure that should be SetInt and not GetInt.

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 Bonfire-Boy · Jul 07, 2015 at 02:20 PM 0
Share

It also seems strange to be storing a float as an int. The user will see the record being broken, only to find that the new record isn't being saved,

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with the Playerprefs 1 Answer

update highscore if current is higher than previous score 0 Answers

[SOLVED] A Playerpref in one place is blocking all my other Playerprefs 1 Answer

Playerprefs not work!? 0 Answers

[SOLVED] Same script (virtually) on two gameObjects. One saves to playerprefs, one doesn't 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