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 /
  • Help Room /
avatar image
0
Question by Ramonjr · Jun 06, 2020 at 06:00 PM · leaderboard

convert float to int for google leaderboard

Hi

i have a new mode where score is measured by distance, i've got it working fine in game and saving in player pref, but unsure how to report it back to google leaderboard as float...

here is my distance player pref script - working

 float distanceunit = 0;
 float bestDistanceunit;

private void Awake() { if (PlayerPrefs.HasKey("BestDistanceBulking")) { bestDistanceunit = PlayerPrefs.GetFloat("BestDistanceBulking"); }

private void Update() { currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, time / accelerationTime); transform.position += Vector3.right currentSpeed Time.deltaTime; time += Time.deltaTime;

     if (distanceIncreasing)
     {
         distanceunit += pointsPerSecond * Time.deltaTime;
     }
     if (distanceunit > bestDistanceunit)
     {
         bestDistanceunit = distanceunit;
         PlayerPrefs.SetFloat("BestDistanceBulking", bestDistanceunit);
         GPGSLeaderboards.UpdateLeaderboardBestDistanceBulking();
     }

here is part of my gpgsleaderboard script for posting to leaderboard which isnt working..

 public static void UpdateLeaderboardDistance()
 {
     if (PlayerPrefs.GetFloat("BestDistanceBulking", 0) == 0)
     {
         return;
     }

     Social.ReportScore(PlayerPrefs.GetFloat("BestDistanceBulking", 1), GPGSIds.leaderboard_bulk_chokie__distance_, (bool success) =>
     {
         if (success)
         {
             //PlayerPrefs.SetInt("BestDistanceBulking", 0);
         }
     });
 }

Thank you heaps

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
1

Answer by Dangerface · Jun 07, 2020 at 05:18 AM

You could use the 'Mathf.RoundToInt' function to round the float however you like
https://docs.unity3d.com/ScriptReference/Mathf.RoundToInt.html

Comment
Add comment · Show 6 · 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 Ramonjr · Jun 07, 2020 at 05:06 PM 0
Share

Thanks, would you be able to advise how i'd do it with my script? thanks

avatar image Dangerface Ramonjr · Jun 07, 2020 at 06:45 PM 0
Share

Sorry, I don't know anything about google leaderboards, so I don't know if your provided code is something you wrote or some standard stuff.


You could try this:

 PlayerPrefs.SetFloat("BestDistanceBulking", $$anonymous$$athf.RoundToInt(bestDistanceunit));

or if you want to store the int in its own variable just convert it and store it globally

 int bestDistInt;
 bestDistInt = $$anonymous$$athf.RoundToInt(bestDistanceunit);



avatar image Ramonjr Dangerface · Jun 08, 2020 at 10:44 AM 0
Share

thank you - this got it working for showing the best distance in the main menu etc. but still having problems reporting the correct distance into the google leaderboard, this is what ive done so far:

Player distance script:

  if (distanceIncreasing)
  {
      distanceunit += pointsPerSecond * Time.deltaTime;
  }
  if (distanceunit > bestDistanceunit)
  {
      bestDistanceunit = distanceunit;
      PlayerPrefs.SetInt("BestDistanceBulking", bestDistanceunit);
      GPGS$$anonymous$$erboards.Update$$anonymous$$erboardBestDistanceBulking();
      bestDistInt = $$anonymous$$athf.RoundToInt(bestDistanceunit);
  }

$$anonymous$$erboard script (it is reporting to the leaderboard but the score is always "1" regardless of my distance

public static void Update$$anonymous$$erboardDistance() { if (!PlayerPrefs.HasKey("BestDistanceBulking")) { return; } Social.ReportScore(PlayerPrefs.GetFloat("BestDistanceBulking", 1), GPGSIds.leaderboard_bulk_chokie_distance, (bool success) => { if (success) { } }); }

Show more comments

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

203 People are following this question.

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

Related Questions

Try to show a string on textfield 0 Answers

OnApplicationPause Not working 0 Answers

Leaderboard not found 0 Answers

local player ranking system 0 Answers

Leaderboard shows an error with Google Play Services 0 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