C# 我怎样才能取得高分?

C# 我怎样才能取得高分?,c#,unity3d,C#,Unity3d,我正在做一个生存射击游戏,我试图让它,当我杀死一个敌人,它增加了我的分数。分数将添加到高分,并在每次播放后保存。我有增加分数的脚本,但我不认为我做得很好。有人能帮我吗 public class ScoreManager : MonoBehaviour { public static int score; public static int highScore; Text scoreText; Text highScoreText; void Awake

我正在做一个生存射击游戏,我试图让它,当我杀死一个敌人,它增加了我的分数。分数将添加到高分,并在每次播放后保存。我有增加分数的脚本,但我不认为我做得很好。有人能帮我吗

public class ScoreManager : MonoBehaviour
{
    public static int score;
    public static int highScore;

    Text scoreText;
    Text highScoreText;

    void Awake ()
    {
        scoreText = GetComponent <Text> ();
        highScoreText = GetComponent<Text>();
        score = 0;
        highScore = 0;

        highScoreText.text = PlayerPrefs.GetInt("High Score", 0).ToString();
    }
    void Update ()
    {
        scoreText.text = "Score: " + score;
        highScoreText.text = "High Score: " + highScore;
        if(score>PlayerPrefs.GetInt("High Score", 0))
        {
            PlayerPrefs.SetInt("High Score", score);
            highScoreText.text = score.ToString();
        }
    }
}  
这是敌人死亡时增加分数的生命脚本。 它在敌人的思考功能中

public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;
    public int currentHealth;
    public float sinkSpeed = 2.5f;
    public int scoreValue = 10;
    public AudioClip deathClip;


    Animator anim;
    AudioSource enemyAudio;
    ParticleSystem hitParticles;
    CapsuleCollider capsuleCollider;
    bool isDead;
    bool isSinking;


    void Awake ()
    {
        anim = GetComponent <Animator> ();
        enemyAudio = GetComponent <AudioSource> ();
        hitParticles = GetComponentInChildren <ParticleSystem> ();
        capsuleCollider = GetComponent <CapsuleCollider> ();

        currentHealth = startingHealth;
    }


    void Update ()
    {
        if(isSinking)
        {
            transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
        }
    }


    public void TakeDamage (int amount, Vector3 hitPoint)
    {
        if(isDead)
            return;

        enemyAudio.Play ();

        currentHealth -= amount;

        hitParticles.transform.position = hitPoint;
        hitParticles.Play();

        if(currentHealth <= 0)
        {
            Death ();
        }
    }


    void Death ()
    {
        isDead = true;

        capsuleCollider.isTrigger = true;

        anim.SetTrigger ("Dead");

        enemyAudio.clip = deathClip;
        enemyAudio.Play ();
    }


    public void StartSinking ()
    {
        GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;
        GetComponent <Rigidbody> ().isKinematic = true;
        isSinking = true;
        ScoreManager.score += scoreValue;
        Destroy (gameObject, 2f);
    }
}  
但我面临两个问题:

当我重新启动游戏时,高分文本将替换分数文本 当我杀死敌人时,分数文本不会更新
没有必要每一帧都不断更新highscore,你只需要在游戏结束时保存新的highscore。您可以在游戏开始时获得高分,然后在游戏期间使用该值,当游戏结束时,您更新高分值

你也从来没有给你的高分赋值,它总是0。将两个文本组件都放在公共位置并将它们添加到检查器中也会更容易

public class ScoreManager : MonoBehaviour
{
public static int score;
public static int highScore;

public Text scoreText;
public Text highScoreText;

void Awake()
{
    score = 0;
    highScore = PlayerPrefs.GetInt("High Score", 0);

    highScoreText.text = "High Score: " + highScore;
}
void Update()
{
    scoreText.text = "Score: " + score;
    highScoreText.text = "High Score: " + highScore;
    if (score > highScore)
    {
        highScoreText.text = "High Score: " + score;
    }
}
}
然后,在游戏结束的任何地方添加保存高分的代码。

根据您的评论:

当我停止比赛并再次比赛时,高分就在球场上 屏幕但是当我玩的时候,高分文字取代了分数文字, 当我杀死一个敌人的时候,它不算在内

当你杀死敌人时,你的分数没有增加的原因是,当你的敌人死亡时,你没有呼叫StartKing。正如我在该方法中看到的,您可以更改ScoreManager.score的值

当我停止游戏并再次玩时,屏幕上会显示高分。但是当我玩的时候,高分文字取代了分数文字,当我杀死敌人时,它不算在内

代码中有两个GetComponent。它们似乎引用了相同的组件,导致您的高分覆盖了分数文本。尝试创建空游戏对象来保存每个文本元素,然后使用公共文本scoreText和公共文本highScoreText,并在Unity编辑器中将这两个游戏对象拖动到脚本中的适当位置

例如:

此外,你实际上并没有更新你的高分。由于文本组件依赖于基础分数变量,因此不能从文本开始,将高分保留为0。您需要确保更新变量,然后根据变量更新文本,否则您可以以高分开始文本,但在下一次更新中,由于高分尚未更新,文本会立即重写为0

您需要的应该是以下内容:

清醒时:

更新中:

游戏结束后:

int originalHighScore = PlayerPrefs.GetInt("High Score", 0);
// compare current session's high score vs our recorded high score from prefs
if (highScore > originalHighScore)
{
    PlayerPrefs.SetInt("High Score", highScore);
}

你为什么认为你做得不对?这个代码有什么问题?在调试代码时,您需要更具体地说明您注意到了什么您的问题是什么?有什么问题?添加分数的代码在哪里?我只是不知道如何存储我的高分。我刚刚添加了添加分数的脚本。我想知道我是否必须将我的高分设置为等于我的分数,然后设置一个条件,如果分数大于高分,我应该停止连接它们。请澄清您的预期结果是什么以及现在出了什么问题
void Death ()
{
    isDead = true;

    capsuleCollider.isTrigger = true;

    anim.SetTrigger ("Dead");

    enemyAudio.clip = deathClip;
    enemyAudio.Play ();
    StartSinking();
}
// get high score from PlayerPrefs
highScore = PlayerPrefs.GetInt("High Score", 0);
void Update ()
{
    scoreText.text = "Score: " + score;
    if (score > highScore)
    {
        highScore = score;
        // only update high score text when it actually changes
        highScoreText.text = "High Score: " + highScore;
    }
}
int originalHighScore = PlayerPrefs.GetInt("High Score", 0);
// compare current session's high score vs our recorded high score from prefs
if (highScore > originalHighScore)
{
    PlayerPrefs.SetInt("High Score", highScore);
}