如何比较第二个结果和第一个结果

时间:2015-12-10 17:52:48

标签: c# unity3d scripting compare

我对这个问题感到非常沮丧,因为我不知道如何做到这一点。我在计算积分,我想保存最高分。这很简单,看看我的剧本:

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保存高分。但是,我不知道如何在第二次有人玩游戏时将得分1与新得分(得分2)进行比较。例如,如果有人第一次获得100分,第二次获得200分,那么如何让游戏显示200分的高分?非常感谢你!

感谢您的努力!我尝试使用它,但我遇到了许多我无法解决的错误:(1:Assets / POINTS1.cs(22,25):错误CS1061:输入UnityEngine.UI.Text' does not contain a definition for文本'而没有扩展方法{{1可以找到UnityEngine.UI.Text'(你是否缺少using指令或汇编引用?)2:Assets / POINTS1.cs(33,44):错误CS1061:输入Text' of type toString'而不是可以找到扩展方法int' does not contain a definition for int'(您是否缺少using指令或程序集引用?)3:Assets / POINTS1.cs(33,25):error CS1061:Type toString' of type Text'和无法找到扩展方法UnityEngine.UI.Text' does not contain a definition for UnityEngine.UI.Text'(您是否缺少using指令或程序集引用?)4:Assets / POINTS1.cs(37,27):错误CS1061:类型{{1可以找到文本'和没有扩展方法Text' of type UnityEngine.UI.Text'(你是否缺少using指令或程序集引用?)5:Assets / POINTS1.cs(39,27):错误CS1061 :键入UnityEngine.UI.Text' does not contain a definition for文本'并且找不到扩展方法Text' of type UnityEngine.UI.Text'(您是否缺少using指令或程序集引用?)。当我有更多时间我会尝试自己解决错误。

2 个答案:

答案 0 :(得分:1)

您永远不会设置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

答案 1 :(得分:1)

我对您的代码进行了更改,但无法对其进行测试,但我认为这会对您有所帮助:

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);
    }


}