前提・実現したいこと
プレイシーンでのクリアタイムを別のシーンでランキングで表示したいと考えています。
PlayerPrefsの保存、ロードを用いて作りたいと思っています。
ランキングでの表示は0:00の形です。
発生している問題・エラーメッセージ
2位以下に記録が出ず、0:00から変わらない。
2回以上ランキングのシーンに移動しないと1位の表示が0:00から変わらない。
該当のソースコード
C#
1 public GameObject score_object; 2 public GameObject score_object2; 3 public GameObject score_object3; 4 public float score1; 5 float minute1; 6 public float time; 7void Start(){ 8 time = Moveforward.Gettime(); //プレイシーンでの時間をここで受け取る 9 score1 = PlayerPrefs.GetFloat("1位", score1); 10 score2 = PlayerPrefs.GetFloat("2位", score2); 11 score3 = PlayerPrefs.GetFloat("3位", score3); 12 } 13 void OnDestroy(){ 14 if (score1 == 0){ //一回目のプレイで必ず1位にスコアが入るように 15 score1 = time; 16 PlayerPrefs.SetFloat("1位", score1); 17 PlayerPrefs.SetFloat("2位", score2); 18 PlayerPrefs.SetFloat("3位", score3); 19 PlayerPrefs.Save(); 20 } 21 else if (time < score1){ //タイムが1位よりも早い時 22 score3 = score2; 23 score2 = score1; 24 score1 = time; 25 PlayerPrefs.SetFloat("1位", score1); 26 PlayerPrefs.SetFloat("2位", score2); 27 PlayerPrefs.SetFloat("3位", score3); 28 PlayerPrefs.Save(); 29 } 30 else if (time < score2){ //タイムが2位よりも早い時 31 score3 = score2; 32 score2 = time; 33 PlayerPrefs.SetFloat("2位", score2); 34 PlayerPrefs.SetFloat("3位", score3); 35 PlayerPrefs.Save(); 36 } 37 else if (time < score3){ //タイムが3位よりも早い時 38 score3 = time; 39 PlayerPrefs.SetFloat("3位", score3); 40 PlayerPrefs.Save(); 41 } 42 } 43void Update(){ 44 TextMesh score_text = score_object.GetComponent<TextMesh>(); 45 TextMesh score_text2 = score_object2.GetComponent<TextMesh>(); 46 TextMesh score_text3 = score_object3.GetComponent<TextMesh>(); 47 48 //ここからはタイムの表示を0:00にするために 49 if (score1 >= 60){ //score2、score3も同様に 50 minute1++; 51 score1 = score1 - 60; 52 } 53 if (score1 < 10){ //2位、3位も同様に 54 score_text.text = "1位: " + minute1 + ":0" + score1; 55 } 56 else{ 57 score_text.text = "1位: " + minute1 + ":" + score1; 58 } 59 }
試したこと
timeもPlayerPrefsで保存しての方法を試したのですが変わりませんでした。
自力で使えるのがif文のみのため長くなっていますが、よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/23 07:24