前提・実現したいこと
C#初心者です。
2つのテキストにそれぞれTime.timeコマンドを使った時間計測のプログラムをつけています。
(Text1←Timer Text2←Timer2)
このプログラムはTimerでは左マウスボタン、Timer2では右マウスボタンを押すと時間が止まる予定になっています。
ですが、いざ実行した際にどちらかのボタンを押しても時間が止まらずにずっと進んでしまいます。
また、片方のテキストだけにした場合はボタンを押したら時間が止まります。
左ボタンを押したらText1に表示している時間が止まり、右ボタンを押したらText2に表示している時間が止まるようにして欲しいです。
発生している問題・エラーメッセージ
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Timer : MonoBehaviour { private Text timerText; private int minute; private float seconds; private float oldSeconds; // 最初の時間 private float startTime; // Use this for initialization void Start() { timerText = GetComponentInChildren<Text>(); oldSeconds = 0; startTime = Time.time; } // Update is called once per frame void Update() { // Time.timeでの時間計測 seconds = Time.time - startTime; minute = (int)seconds / 60; if ((int)seconds != (int)oldSeconds) { timerText.text = minute.ToString("00") + ":" + ((int)(seconds % 60)).ToString("00"); } oldSeconds = seconds; // マウスの左ボタン押しで一時停止 if (Input.GetMouseButtonDown(0)) { Time.timeScale = Time.timeScale == 0 ? 1 : 0;//未定 } } } -------------------------------------------------------------------- using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Timer2 : MonoBehaviour { private Text timerText2; private int minute2; private float seconds2; private float oldSeconds2; // 最初の時間 private float startTime2; // Use this for initialization void Start () { timerText2 = GetComponentInChildren<Text>(); oldSeconds2 = 0; startTime2 = Time.time; } // Update is called once per frame void Update () { // Time.timeでの時間計測 seconds2 = Time.time - startTime2; minute2 = (int)seconds2 / 60; if ((int)seconds2 != (int)oldSeconds2) { timerText2.text = minute2.ToString("00") + ":" + ((int)(seconds2 % 60)).ToString("00"); } oldSeconds2 = seconds2; // マウスの左ボタン押しで一時停止 if (Input.GetMouseButtonDown(1)) { Time.timeScale = Time.timeScale == 0 ? 1 : 0; } //while (true) {} } /* [SerializeField] private int minute2; [SerializeField] private float seconds2; // 前のUpdateの時の秒数 private float oldSeconds2; // タイマー表示用テキスト private Text timerText2; void Start() { minute2 = 0; seconds2 = 0f; oldSeconds2 = 0f; timerText2 = GetComponentInChildren<Text>(); } void Update() { seconds2 += Time.deltaTime; if (seconds2 >= 60f) { minute2++; seconds2 = seconds2 - 60; } // 値が変わった時だけテキストUIを更新 if ((int)seconds2 != (int)oldSeconds2) { timerText2.text = minute2.ToString("00") + ":" + ((int)seconds2).ToString("00"); } oldSeconds2 = seconds2; } */
該当のソースコード
C#
1 2 [SerializeField] 3 private int minute2; 4 [SerializeField] 5 private float seconds2; 6 // 前のUpdateの時の秒数 7 private float oldSeconds2; 8 // タイマー表示用テキスト 9 private Text timerText2; 10 11 void Start() 12 { 13 minute2 = 0; 14 seconds2 = 0f; 15 oldSeconds2 = 0f; 16 timerText2 = GetComponentInChildren<Text>(); 17 } 18 19 void Update() 20 { 21 seconds2 += Time.deltaTime; 22 if (seconds2 >= 60f) 23 { 24 minute2++; 25 seconds2 = seconds2 - 60; 26 } 27 // 値が変わった時だけテキストUIを更新 28 if ((int)seconds2 != (int)oldSeconds2) 29 { 30 timerText2.text = minute2.ToString("00") + ":" + ((int)seconds2).ToString("00"); 31 } 32 oldSeconds2 = seconds2; 33 } 34
試したこと
上記のように、片方の時間計測のソースをTime.timeではなくTime.deltaTimeにして実行したところ両方の時間が止まりました。
Time.deltaTimeの方は時間を止めるプログラムをしていないのにです。
補足情報(FW/ツールのバージョンなど)

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。