簡體   English   中英

如何比較第二個結果和第一個結果

[英]How to compare 2nd result to 1st result

我對這個問題感到非常沮喪,僅僅是因為我不知道如何實現。 我正在計算分數,我想保存最高分數。 這很簡單,看一下我的腳本:

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

public class POINTS1 : MonoBehaviour
{

    public Text countText;
    public Text winText;
    public AudioSource pickUpAudio;
    public AudioSource minus300Audio;

    public int score1;
    public int score2;
    public Text scoreText;

    private int count;


    void Start()
    {

        count = 0;
        SetCountText();
        winText.text = "";
        PlayerPrefs.SetInt("score", count);
        PlayerPrefs.Save();
        count = PlayerPrefs.GetInt("score", 0);

        PlayerPrefs.GetInt("scorePref");
        score1 = PlayerPrefs.GetInt("scorePref");
    }

    void Update()
    {
        if (scoreText.name == "scoreText")
        {
            scoreText.text = "HS: " + score1;
        }
        PlayerPrefs.SetInt("scorePref", score1);

    }


    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Pickup"))
        {
            other.gameObject.SetActive(false);
            count = count + 100;


            SetCountText();
            {
                pickUpAudio.Play();
            }
        }


        else if (other.gameObject.CompareTag("minus300"))
        {
            other.gameObject.SetActive(false);
            count = count - 300;



            SetCountText();
            {
                minus300Audio.Play();
            }
        }

        PlayerPrefs.SetInt("score", count);
        PlayerPrefs.Save();
        count = PlayerPrefs.GetInt("score", 0);




    }

    void SetCountText()
    {
        PlayerPrefs.SetInt("score", count);
        PlayerPrefs.Save();
        count = PlayerPrefs.GetInt("score", 0);
        countText.text = "Score: " + count.ToString();
        if (count >= 5000)
        {
            winText.text = "Good Job!";
        }
            score1 = count;

        if (score1 > count);
        {
            scoreText.text = "HS: " + score1;
        }

        if (score2 > score1);
        {
            scoreText.text = "Score2> 1 " + score1;
        }
    }


    }

因此,保存第一得分很容易。 我使用int Score1保存了高分。 但是,我不知道如何在有人第二次玩游戲時將score1與新分數(score2)進行比較。 例如,如果某人第一次獲得100分,第二次獲得200分,我如何使游戲顯示200分? 非常感謝!

感謝您的努力! 我嘗試使用它,但出現了很多似乎無法修復的錯誤:( 1:1:Assets / POINTS1.cs(22,25):錯誤CS1061:類型UnityEngine.UI.Text' does not contain a definition for Text' UnityEngine.UI.Text' does not contain a definition for並且沒有找到UnityEngine.UI.Text Text' of type擴展方法Text' of type (您是否缺少using指令或程序集引用?)2:Assets / POINTS1.cs(33,44):error CS1061:Type int' does not contain a definition for toString' int' does not contain a definition for並且找不到int' toString' of type擴展方法toString' of type (您是否缺少using指令或程序集引用?)3:Assets / POINTS1.cs(33,25):錯誤CS1061:類型UnityEngine.UI.Text' does not contain a definition for Text UnityEngine.UI.Text' does not contain a definition for也找不到Text' of type UnityEngine.UI.Text'的擴展方法Text' of type (您是否缺少using指令或程序集引用?)4:資產/ POINTS1。 cs(37,27):錯誤CS1061:類型UnityEngine.UI.Text' does not contain a definition for Text' UnityEngine.UI.Text' does not contain a definition for並且找不到Text' of type UnityEngine.UI.Text'的擴展方法Text' of type (是否缺少using指令或一個屁股 嵌入參考嗎?)5:資產/POINTS1.cs(39,27):錯誤CS1061:類型UnityEngine.UI.Text' does not contain a definition for Text' UnityEngine.UI.Text' does not contain a definition for並且沒有Text' of type UnityEngine.UI.Text'的擴展方法Text' of type被找到(您是否缺少using指令或程序集引用?)。 當我有更多時間時,我將嘗試自己修復錯誤。

您永遠不會設置score2,並且score1始終等於代碼中的count。 我相信這些變化將為您提供所需的東西。

    score2 = count;

    if (score2 > score1);
    {
        score1 = score2;
    }

    scoreText.Text = $"HS: {score1}";

    // EDIT: After doing the comparison, score1 needs to be the HIGH score

我對您的代碼進行了更改,無法對其進行測試,但是我認為這將對您有所幫助:

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

public class POINTS1 : MonoBehaviour
{
    public Text winText;
    public AudioSource pickUpAudio;
    public AudioSource minus300Audio;

    public int actualScore;
    public int highScore;
    public Text highscoreText;
    public Text actualScoreText;

    private int count;


    void Start()
    {
        highScore = PlayerPrefs.HasKey("highScore") ? PlayerPrefs.GetInt("highScore") : 0; //This will verify if the key highScore exists in your player prefs, if it exists it should retrieve the stored value if not will return 0;
        actualScoreText.text = "0";
    }

    void Update()
    {
        // if (scoreText.name == "scoreText") //If you are going to set this in the editor, you don't need to verify the name of the object...
        // {
        // scoreText.text = "HS: " + highScore;
        // }

        //Set text for actualScore and highScore (notice that I've created another variable to display the actual score vs the highScore)
        actualScoreText.text = actualScore.ToString() ;
        // We compare the actual score vs the highScore if the actual is greater the we set it to the text variable.
        if (actualScore > highScore)
            highscoreText.text = "HS: " + actualScore;
        else
            highscoreText.text = "HS: " + highScore;
    }


    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Pickup"))
        {
            other.gameObject.SetActive(false);
            actualScore += 100;
            {
                pickUpAudio.Play();
            }
        }
        else if (other.gameObject.CompareTag("minus300"))
        {
            other.gameObject.SetActive(false);
            actualScore -= 300;
            minus300Audio.Play();
//If you want your score not to go below 0, uncomment the next if statement
            //if (actualScore < 0) 
            //{
            //    actualScore = 0;    //With this line your score can't be under 0
            //}
        }

    }

    //I don't know why you called this code many times in the OnTriggerEnter() ....
    // void SetCountText()
    // {
    // PlayerPrefs.SetInt("score", count);
    // PlayerPrefs.Save();
    // count = PlayerPrefs.GetInt("score", 0);
    // countText.text = "Score: " + count.ToString();
    // if (count >= 5000)
    // {
    // winText.text = "Good Job!";
    // }
    // score1 = count;

    // if (score1 > count);
    // {
    // scoreText.text = "HS: " + score1;
    // }

    // if (score2 > score1);
    // {
    // scoreText.text = "Score2> 1 " + score1;
    // }
    // }

    void GameOver() //Call GameOver whenever your game is... over, let say .. when your player got N amount of points or N amount of damage...
    {
        if (actualScore >= 5000)
            winText.text = "Good Job";

        if (actualScore >= highScore)
            PlayerPrefs.SetInt("highScore", actualScore);
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM