2017-02-26 53 views
0

我需要從我的其他班級獲取變量分數並將其設置爲UserPrefs鍵。它似乎並沒有設置,因爲如果沒有UserPrefs鍵「Highscore」,我有一個GUI標籤遊戲名稱,其中顯示「無」。保存並顯示整個班級變量的高分統一

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

using System.Threading; 

public class Player : MonoBehaviour { 

public Vector2 jumpForce = new Vector2(0, 300); 
private Rigidbody2D rb2d; 
Generator SwagScript; 
GameObject generator; 



// Use this for initialization 
void Start() { 
    rb2d = gameObject.GetComponent<Rigidbody2D>(); 
    generator = GameObject.FindGameObjectWithTag("Generator"); 
    SwagScript = generator.GetComponent<Generator>(); 
} 

// Update is called once per frame 
void Update() { 
    if (Input.GetKeyUp("space")) 
    { 
     rb2d.velocity = Vector2.zero; 
     rb2d.AddForce(jumpForce); 
    } 

    Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position); 
    if (screenPosition.y > Screen.height || screenPosition.y < 0) 
    { 
     Die(); 
    } 
} 

void OnCollisionEnter2D(Collision2D other) 
{ 
    Die(); 
} 

void Die() 
{ 
    if (PlayerPrefs.HasKey("HighScore")) 
    { 
     if (PlayerPrefs.GetInt("Highscore") < SwagScript.score) 
     { 
      PlayerPrefs.SetInt("HighScore", SwagScript.score); 
     } 
     else 
     { 
      PlayerPrefs.SetInt("HighScore", SwagScript.score); 
     } 
    } 
    Application.LoadLevel(Application.loadedLevel); 
} 

}

回答

0

除非你正在創建的Highscore playerpref其他地方,該Die()方法不是沒有創造它,因爲if (PlayerPrefs.HasKey("HighScore"))將始終返回false。

只要刪除,如果。

+0

謝謝隊友我會這麼做的! –