- Home /
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?
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.
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.
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.
Your answer
Follow this Question
Related Questions
Score display in specific format 1 Answer
Score added on collision with another player 0 Answers
scoreboard for multiplayer game 0 Answers
Creating a scoreboard 1 Answer