我正在製作一個生存射擊遊戲,並且我試圖讓它如此,當我殺死一個敵人時,它爲我的分數增加了分數。得分被添加到高分,並在每次播放後被保存。我有腳本來增加分數,但我認爲我沒有正確地做出高分。有人可以幫我解決這個問題嗎?統一 - 如何獲得高分?
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();
}
}
}
這裏是敵人死亡時增加敵人生命值。 這是在enemySinking功能。
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);
}
}
但我面臨兩個問題:
- 當我重新開始遊戲,高分文本替換分數文本
- 當我殺死敵人的分數文本不更新
w ^你認爲你做得不對嗎?這段代碼有什麼問題?你需要更具體,並顯示你在調試代碼時注意到什麼 – UnholySheep
你的問題是什麼?問題是什麼?添加分數的代碼在哪裏? – FCin
我只是不確定如何存儲我的高分。我剛剛添加了添加分數的腳本。 –