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 Nabagielis · Feb 05, 2013 at 06:52 PM · scorescoreboard

Saving travelled distance as a score

Hello im making a runner game and I am at the part where I have set up my score system. For my game the score will be the distance you have travelled. I want to know how could I save the distance I travelled in my level scene and show it in my game over scene. Here is the full script that Im using to get my travelled distance.

 using UnityEngine;
 using System.Collections;
  
 public class Runner : MonoBehaviour {
  
     public static float distanceTraveled;
  
     public float acceleration;
     public Vector3 boostVelocity, jumpVelocity;
     public float gameOverY;
  
     private bool touchingPlatform;
     
     private static int boosts;
  
     void Update () {
        if(Input.GetButtonDown("Jump")){
             if(touchingPlatform){
          rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
          touchingPlatform = false;
                 }
             else if(boosts > 0){
                 rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
                 boosts -= 1;
                 GUIManager.SetBoosts(boosts);
             }
        }
        distanceTraveled = transform.localPosition.x;
         GUIManager.SetDistance(distanceTraveled);
  
        if(transform.localPosition.y < gameOverY)
     StartCoroutine("DelayedLevelLoad");
     }
                 
             IEnumerator DelayedLevelLoad(){
           yield return new WaitForSeconds(1);
           Application.LoadLevel("GameOver");
          }
             
  
     void FixedUpdate () {
        if(touchingPlatform){
          rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
        }
     }
  
     void OnCollisionEnter () {
        touchingPlatform = true;
     }
  
     void OnCollisionExit () {
        touchingPlatform = false;
     }
     
     private void GameStart () {
         boosts = 0;
         GUIManager.SetBoosts(boosts);
         distanceTraveled = 0f;
         GUIManager.SetDistance(distanceTraveled);
         
     }
     
     public static void AddBoost(){
         boosts += 1;
         GUIManager.SetBoosts(boosts);
     }
 }

Right now the only way of knowing my score is just that I can see it in the top part of my screen, but when I die I cant check anywhere what my score was. So any ideas on how I could do this?

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 LightSource · Feb 05, 2013 at 08:05 PM 0
Share

Will you be recording the actual distance or just running forward? If its anything like temple run you can just set a variable to count up then display itself as GUI.

avatar image Nabagielis · Feb 05, 2013 at 08:27 PM 0
Share

Im just running in one direction cause its a 2d runner and I can already see my traveled distance on the GUI I have set up. I just want to be able to somehow save the traveled distance so that I could see it in the game over scene that opens when you fall off a platform. I would like to also be able to see my best scores.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Feb 05, 2013 at 08:55 PM

Three methods:

  • Use Object.DontDestroyOnLoad() so a particular object that has a script attached containing the information you need is not destoryed on the new scene load.

  • Use PlayerPrefs to store the information. This store is permanent (i.e. it remains after the game is quit), so if you want each game to start at zero, you will need to set it to zero at the beginning of hte game.

  • Use a Singleton. There is an example on this page showing how to track a score.

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

10 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

Related Questions

Score display in specific format 1 Answer

my code is suppose to store high score and its storing recent score insted and it wont stop if game over and it has an score counter as well plzz help 0 Answers

Score added on collision with another player 0 Answers

scoreboard for multiplayer game 0 Answers

Creating a scoreboard 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