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
1
Question by Blood_Rush · Jan 17, 2014 at 10:40 PM · scoring

How do I keep the highest scoring value for a game session?

Hi guys.

I have a problem with my little scoring system. Wonder if u guys can help me out. Once again, I've been following the tutorials on catlikecoding.com to sharpen up my C# skills.

My scoring system is based on how far a player has traveled (like the function used in the tutorial)

In my Runner.cs Script:

 distanceTraveled = transform.localPosition.x;
 GUIManager.SetDistance (distanceTraveled);

Now I have managed to add a new variable for high score on a guitext which displays my last score achieved after the game over screen and shows up when u reset the scene. But, here's my problem and concern : Whenever the game ends now and the scene resets it uses the previous score achieved and doesn't keep the highest value achieved, meaning I might was well rename it to previous score instead of high score.

In my Runner.cs Script for setting score:

 highScore = distanceTraveled;

 If (distanceTraveled > highScore) {
     GUIManager.SetScore (highScore);
 }

 else if (distanceTraveled < highScore) {
 //(I haven't set anything here as I'm not sure wtf I can use here, and believe me I have tried)
 }

In my GUIManager.cs Script:

 public static void SetScore (float highScore) {
     instance.highScore.text = highScore.ToString ("f0");
 }

So my question is, how do I keep the highest distance traveled value in my high score variable for the remainder of that game session?

PS: Once again, if I don't make any sense, just curse me. Have to do this from my phone again seeing as this god forsaken country haven't installed my internet line yet.

Thanks in advance.

Comment
Add comment · Show 2
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 KellyThomas · Jan 17, 2014 at 10:49 PM 0
Share

Hi @Blood_Rush it's important to format your code correctly so that we can read your question more easily. I've helped out for this question but try to bear it in $$anonymous$$d for the future.

avatar image Blood_Rush · Jan 18, 2014 at 07:10 AM 0
Share

Sorry about that. I'm using this dumbass blackberry phone of $$anonymous$$e to post things, and sometimes I'm not near my pc to remember what my code looks like.

Thanks for helping me out with that.

2 Replies

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

Answer by KellyThomas · Jan 17, 2014 at 10:54 PM

This test will always fail. By assigning the same value to two different variables it is impossible for one to be greater than the other.

 highScore = distanceTraveled;
 
 if (distanceTraveled > highScore) {
     GUIManager.SetScore (highScore);
 }


Instead use this:

 if (distanceTraveled > highScore) {
     highScore = distanceTraveled;
     GUIManager.SetScore (highScore);
 }

Just be sure to initialize highscore (to 0 or the saved highscore) in Start()/Awake() or somewhere else outside of the update loop.

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 Blood_Rush · Jan 18, 2014 at 07:16 AM 0
Share

Ok, thanks. Let me just pick up my brain from the floor. Haha. Nah, it's all good. I understand some of the logic. I'm just playing it in my head how to incorporate all of it into the current code. But thanks for that. I appreciate the help. I will get back to you if I require assistance.

avatar image
0

Answer by fuzzysmurf · Jan 17, 2014 at 10:57 PM

Im not to sure how to do this in Unity, I believe you just transfer the int (or float) between scripts. otherwise, you can create a CSV file as an alternative (using File.writeLine(location, string)), and it will always contain the values for you. if you do try this, you will need "Using System.IO", although I would only recommend this as a last resort. I'm sure Unity has a built in method, or an alternative to approaching this.

Comment
Add comment · Show 2 · 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 KellyThomas · Jan 17, 2014 at 10:58 PM 1
Share

PlayerPrefs is the supplied solution.

avatar image Blood_Rush · Jan 18, 2014 at 07:03 AM 0
Share

I don't want to get too technical as this is but a basic game and also my first time getting this far developing sumthing from scratch (with a little bit of help from the catlikecoding.com tutorial.) $$anonymous$$ost of the games I made was for friends and was just a learning experience, but I just dunked a fps controller in, built my environment and modelling architecture. I'm not a strong programmer, although I have done a little bit of php. The PlayerPrefs seems like a good idea, I just have to basically instance my travel distance to it. So thanks for that. Although its gonna take me a while to figure it out.

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

20 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

Related Questions

SHMUP - Scoring and Scripting Help 1 Answer

How to save score for survival time? 1 Answer

How to set your numbers to expand the opposite direction? 1 Answer

Score System? 0 Answers

saving scores and let other user to view it 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