如何将第二个结果与第一个结果进行比较
本文关键字:结果 比较 第一个 第二个 | 更新日期: 2023-09-27 18:26:28
我对这个问题感到非常沮丧,因为我不知道如何做到这一点。我正在计算积分,我想保存最高分。这很简单,看看我的脚本:
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:资产/POINTS1.cs(22,25(:错误 CS1061:键入 UnityEngine.UI.Text' does not contain a definition for
Text',但找不到 UnityEngine.UI.Text' Text' of type
扩展方法(是否缺少 using 指令或程序集引用? 2:资产/POINTS1.cs(33,44(:错误 CS1061:键入 int' does not contain a definition for
toString' 并且找不到 int' toString' of type
扩展方法(是否缺少 using 指令或程序集引用? 3: 资产/POINTS1.cs(33,25(:错误 CS1061:键入 UnityEngine.UI.Text' does not contain a definition for
Text',但找不到 UnityEngine.UI.Text Text' of type
扩展方法(是否缺少 using 指令或程序集引用? 4:资产/POINTS1.cs(37,27(:错误 CS1061:键入UnityEngine.UI.Text' does not contain a definition for
文本",找不到Text' of type
UnityEngine.UI.Text' 的扩展方法(是否缺少 using 指令或程序集引用? 5:资产/POINTS1.cs(39,27(:错误 CS1061:键入 UnityEngine.UI.Text' does not contain a definition for
Text',并且找不到 UnityEngine.UI.Text Text' of type
扩展方法(是否缺少 using 指令或程序集引用?当我有更多的时间时,我会尝试自己修复错误。
我对你的代码进行了更改,无法测试它们,但我认为这将帮助你:
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);
}
}
您永远不会设置 score2,并且 score1 始终等于代码中的计数。 我相信这些变化会让你得到你想要的东西。
score2 = count;
if (score2 > score1);
{
score1 = score2;
}
scoreText.Text = $"HS: {score1}";
// EDIT: After doing the comparison, score1 needs to be the HIGH score