前提・実現したいこと
UnityでC#を使ってResultシーンのTextにMainシーンの結果を表示したいです。敵から何秒逃げれるか、というゲームを作っていて、敵に捕まってしまったらResultシーンに移動して逃げた秒数をResultにTextで表示したいです。すでにMainの画面でストップウォッチの機能とResultシーンに移動するスクリプトはできているので、捕まった時の時間を表示したいです。
(イメージ)
あなたの記録は(あなたが逃げた時間は)
00 : 00秒です
発生している問題・エラーメッセージ
A field initializer cannot reference the non-static field, method, or property 'WatchTime.seconds' secondsが使えない、というのはわかるのですがseconds以外で結果を表すにはどうしたらいいのでしょうか? 下のスクリプトだと、ResultのTextにはScore: 0 という表示になってしまいます
該当のソースコード
C#
1//WatchTime 2 3using UnityEngine; 4using System.Collections; 5using UnityEngine.UI; 6 7public class WatchTime : MonoBehaviour { 8 9 [SerializeField] 10 public static int minute; 11 [SerializeField] 12 private float seconds; 13 // 前のUpdateの時の秒数 14 private float oldSeconds; 15 // タイマー表示用テキスト 16 private Text timerText; 17 18 public static int score = seconds; 19 20 public static int getScore() { 21 return score; 22 } 23 24 void Start () { 25 minute = 0; 26 seconds = 0f; 27 oldSeconds = 0f; 28 timerText = GetComponentInChildren<Text> (); 29 } 30 31 void Update () { 32 seconds += Time.deltaTime; 33 if(seconds >= 60f) { 34 minute++; 35 seconds = seconds - 60; 36 } 37 // 値が変わった時だけテキストUIを更新 38 if((int)seconds != (int)oldSeconds) { 39 timerText.text = minute.ToString("00") + ":" + ((int) seconds).ToString ("00"); 40 } 41 oldSeconds = seconds; 42 } 43} 44 45
C#
1//Score Manager 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6 7using UnityEngine.UI; // 追加 8 9public class ScoreManager : MonoBehaviour { 10 11 12 public GameObject score_object = null; // Textオブジェクト 13 public int score_num = 0; //スコア変数 14 // 初期化 15 void Start () { 16 //スコアのロード 17 score_num = PlayerPrefs.GetInt("Score", 0); 18 } 19 //削除時の処理 20 void OnDestroy() { 21 //スコアを保存 22 PlayerPrefs.SetInt("Score", score_num); 23 PlayerPrefs.Save (); 24 } 25 26 // 更新 27 void Update () { 28 // オブジェクトからTextコンポーネントを取得 29 Text score_text = score_object.GetComponent<Text> (); 30 // テキストの表示を入れ替える 31 score_text.text = "Score:" + score_num; 32 33 34 } 35} 36
試したこと
シーン間で共有するためにstatic int scoreをseconds以外にしてもダメでした。
またWatchTimeをtextに埋め込んだりしてもただのストップウォッチがresultに表示されるだけでした。
リンク
こちらの記事の
public int score = 0;
void Awake() { score = 30; }
という部分のscoreを何にすればいいのかがわかりませんでした。
補足情報
使用端末 MacBookAir Unity 2018.1.0f2
回答1件
あなたの回答
tips
プレビュー