Как исправить систему рекордов (наибольшее время) в Unity (C#) - переменная рекорда не устанавливается - PullRequest
0 голосов
/ 21 марта 2020

У меня есть скрипт, который берет и хранит рекорд, который является временем для скрипта. Я ничего не изменил в сценарии, я думаю ..., но он внезапно перестал работать. Я ценю, что он написан с грустью, но это потому, что это проект, который я хотел написать полностью сам. До сих пор я пытался изменить имя playerpref без всякой причины и играть с небольшими кусками кода, однако ничего не получалось

Код:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{

    public static float timer;
    public Text txt;
    private bool death;
    public GameObject manager;
    private string endTime;
    public Text txt2;
    public Text highText;
    public string Highscore;
    public int HighscoreNum;
    public int HighscoreNumMaybe;
    private string minutes;
    private string seconds;
    private string milliseconds;
    public Text beatText;

    private string[] higherTextChoices = new string[] {"And gosh darn that be higher than yo last one of", "which beats your highscore of", "which crushes your highscore of", "which decimates your highscore of", "which laughs at your highscore of"};
    private string[] lowerTextChoices = new string[] { "And gosh darn that be punier than yo score of", "which is lower than your highscore of", "which cowers behind your highscore of", "which gets laughed at by your highscore of", "which gets bullied at your highscore of" };

    private string higherTextChoice;
    private string lowerTextChoice;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(PlayerPrefs.GetString("Highscores"));
        minutes = "00";
        seconds = "00";
        milliseconds = "00";
        HighscoreNumMaybe = 0;
        death = manager.GetComponent<texts>().death;
        highText.text = PlayerPrefs.GetString("Highscores", "00:00:00");
        HighscoreNum = int.Parse(PlayerPrefs.GetString("Highscores").Replace(":",""));
        higherTextChoice = higherTextChoices[Random.Range(0, higherTextChoices.Length)];
        lowerTextChoice = lowerTextChoices[Random.Range(0, lowerTextChoices.Length)];
    }

    // Update is called once per frame
    void Update()
    {
        death = manager.GetComponent<texts>().death;
        if (death != true)
        {
            timer += Time.deltaTime;
            Debug.Log(timer);
            minutes = Mathf.Floor(timer / 60).ToString("00");
            seconds = (timer % 60).ToString("00");
            milliseconds = ((timer * 1000) % 100).ToString("00");

            txt.text = string.Format("{00}:{1}:{2}", minutes, seconds, milliseconds);
        }
        else
        {
            endTime = txt.text;
            txt2.text = endTime;
            string result = endTime.Replace(":", "");
            HighscoreNumMaybe = int.Parse(result);
            if (HighscoreNumMaybe > HighscoreNum)
            {
                PlayerPrefs.SetString("Highscores", string.Format("{00}:{1}:{2}", minutes, seconds, milliseconds));
                beatText.text = higherTextChoice;
            }
            else
            {
                beatText.text = lowerTextChoice;
            }
            minutes = "00";
            seconds = "00";
            milliseconds = "00";
            HighscoreNumMaybe = 0;
            Debug.Log(PlayerPrefs.GetString("Highscores"));
            Debug.Log(HighscoreNum);
        }
    }

    private void OnLevelWasLoaded(int level)
    {
        Debug.Log(PlayerPrefs.GetString("Highscores"));
        timer = 0;
        txt.text = string.Format("{00}:{1}:{2}", minutes, seconds, milliseconds);
    }



}

Заранее спасибо, Оскар.

...