2017-10-13 57 views
0

我正在製作一個生存射擊遊戲,並且我試圖讓它如此,當我殺死一個敵人時,它爲我的分數增加了分數。得分被添加到高分,並在每次播放後被保存。我有腳本來增加分數,但我認爲我沒有正確地做出高分。有人可以幫我解決這個問題嗎?統一 - 如何獲得高分?

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

但我面臨兩個問題:

  1. 當我重新開始遊戲,高分文本替換分數文本
  2. 當我殺死敵人的分數文本不更新
+1

w ^你認爲你做得不對嗎?這段代碼有什麼問題?你需要更具體,並顯示你在調試代碼時注意到什麼 – UnholySheep

+0

你的問題是什麼?問題是什麼?添加分數的代碼在哪裏? – FCin

+0

我只是不確定如何存儲我的高分。我剛剛添加了添加分數的腳本。 –

回答

0

有沒有必要不斷更新每一幀的高分,你應該只在遊戲結束時保存新的高分。您可以在遊戲開始時獲得高分,然後在遊戲過程中使用該值,當遊戲結束時更新高分值

您也從未爲您的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; 
    } 
} 
} 

然後,無論您結束遊戲,添加保存高分的代碼。

1

基於您的評論:

當我停止玩遊戲並再次播放,高分數了 屏幕上。但是,當我玩,高分文本替換文本得分, 而且不計,當我殺死一個敵人

之所以當你殺死敵人你的分數沒有增加是因爲你不當敵人死亡時呼叫StartSinking()。正如我在那個方法看是你改變ScoreManager.score

public void StartSinking() 
{ 
    GetComponent <UnityEngine.AI.NavMeshAgent>().enabled = false; 
    GetComponent <Rigidbody>().isKinematic = true; 
    isSinking = true; 
    ScoreManager.score += scoreValue; 
    Destroy (gameObject, 2f); 
} 

值所以,你應該在某處一個Death()調用添加到StartSinking()

void Death() 
{ 
    isDead = true; 

    capsuleCollider.isTrigger = true; 

    anim.SetTrigger ("Dead"); 

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

當我停止玩遊戲並再次播放,高分是在屏幕上。但是當我玩的時候,高分文本取代了分數文本,並且當我殺死一個敵人時它不算數。

你的代碼中有兩個GetComponent<Text>()。他們好像在引用相同的組件,導致您的高分覆蓋您的分數文本。嘗試創建空的gameobjects來保存每個文本元素,然後使用public Text scoreTextpublic Text highScoreText並在Unity編輯器中將這兩個gameobjects拖放到此腳本中的相應位置。

E.g.Heirarchy,Component

此外,你實際上並沒有更新你的高分。由於您的文本組件依賴於您的基本分數變量,因此您不能以文本開頭並保留最高分數0.您需要確保更新變量,然後根據它們更新文本,否則可以從文本開始得分高,但隨後在下一個Update()中立即重寫爲0,因爲您的高分尚未更新。

像下面這樣的東西應該是你所需要的:

Awake()

// get high score from PlayerPrefs 
highScore = PlayerPrefs.GetInt("High Score", 0); 

Update()

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