2017-02-26 5 views
0

他のクラスから変数スコアを取得し、それをUserPrefsキーとして設定する必要があります。それはUserPrefsキー "Highscore"がない場合、私はGUIラベルingameを持っていて、 "None"をshosしているように設定されているようです。 if (PlayerPrefs.HasKey("HighScore"))は常にfalseを返しますので、クラス全体で変数の高スコアを保存して表示する

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()方法は、それを作成することはありませんではないでしょう。

これを削除してください。

+0

ありがとうございます。私はそれを行います! –

関連する問題