- Home /
Question by
mjking21 · Jul 31, 2020 at 03:22 PM ·
unity 5playerprefsscorescore systemhighscores
in this code whenever score is gretater than highscore then old highscore becomes zero and after the game ends it get updated
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Threading; using System.Globalization; using System.Security.Cryptography.X509Certificates; using System.Net.Sockets;
//Top of the script #pragma warning disable 0649
public class Player : MonoBehaviour {
// Initialize variables.
private Rigidbody rb;
private bool isMovingRight = false;
private bool hasPlayerStarted = false;
public float speed;
public float maxspeed;
[HideInInspector]
private bool canMove = true;
[SerializeField]
GameObject particle;
[SerializeField]
GameObject gems;
[SerializeField]
private Text scoreText;
private int score = 0;
public Text highscoreText;
public int highscore = 0;
public Text ScoreText { get => ScoreText1; set => ScoreText1 = value; }
public bool CanMove { get => CanMove1; set => CanMove1 = value; }
public bool CanMove1 { get => canMove; set => canMove = value; }
public Text ScoreText1 { get => scoreText; set => scoreText = value; }
// Use this for initialization
void Start()
{
rb = this.GetComponent<Rigidbody>();
highscoreText.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && CanMove1 == true)
{
// If the game hasen't started yet.
if (hasPlayerStarted == false)
{
hasPlayerStarted = true;
StartCoroutine(ShowGems(2.5f));
}
ChangeBoolean();
ChangeDirection();
}
if (Physics.Raycast(this.transform.position, Vector3.down * 2) == false)
{
FallDown();
}
// increase speed
if (speed < maxspeed)
speed += 0.01f * Time.deltaTime;
}
// Hide the gems for the first 2.5 seconds (avoiding bugs).
IEnumerator ShowGems(float count)
{
yield return new WaitForSeconds(count);
// Checks if player hasn't fallen off before showing grms.
if (CanMove1 == true)
{
gems.SetActive(true);
}
}
// Control player direction.
private void ChangeBoolean()
{
isMovingRight = !isMovingRight;
}
private void ChangeDirection()
{
if (isMovingRight == true)
{
rb.velocity = new Vector3(speed, 0f, 0f);
}
else
{
rb.velocity = new Vector3(0f, 0f, speed);
}
}
// When the player falls off the platform.
private void FallDown()
{
CanMove1 = false;
rb.velocity = new Vector3(0f, -4f, 0f);
// Retutn to main menu.
StartCoroutine(ReturnToMainMenu(1.8f));
}
// Return to main menu.
IEnumerator ReturnToMainMenu(float count)
{
yield return new WaitForSeconds(count);
SceneManager.LoadScene(0);
}
// When the player hits a gem.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Gem")
{
// Remove the gem.
Destroy(other.gameObject);
// Create the particle effect.
GameObject _particle = Instantiate(particle) as GameObject;
_particle.transform.position = this.transform.position;
Destroy(_particle, 1f);
// Update the score.
score++;
ScoreText1.text = "Score: " + score.ToString();
if (score > PlayerPrefs.GetInt("HighScore", 0))
{
PlayerPrefs.SetInt("HighScore", score);
highscoreText.text = PlayerPrefs.GetInt("score").ToString();
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Trying to get my spawn manager to respond to the current score 1 Answer
How many different scripts do I need to write to create a highscore system? 1 Answer
Recognising high score when updated with a graphic 0 Answers
Highscoring and changing scripts (Java/UnityScript) 1 Answer
How to create a HighScore using playerprefs based on passed time? 1 Answer