- Home /
This question was
closed Jul 31, 2020 at 03:26 PM by
mjking21.
Question by
mjking21 · Jul 26, 2020 at 09:12 AM ·
c#highscores
how to add highscore tab on this code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Threading;
//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 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> ();
}
// 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.1f * 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();
}
}
}
Comment