質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

2回答

256閲覧

時間制限制限で上手く作動しない

YukioMaki

総合スコア21

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2017/10/04 01:20

Unity5.38を使ってC#で下記のスクリプトを書いたのですが、
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Check : MonoBehaviour
{
public GameObject IP;
public InputManager scripta;
public int A;
public GameObject ST;
public Stage2 scripta1;
public Text scoreText;
public int score = 0;
public int T;
private SoundManager sound;
public float time = 0.0f;
void Start()
{
scoreText.text = " ";
}

// Update is called once per frame void Update() { if (Input.anyKey) { sound = GameObject.Find("SoundManager").GetComponent<SoundManager>(); IP = GameObject.Find("InputManager"); scripta = IP.GetComponent<InputManager>(); A = scripta.Answer; score = A; scoreText.text = " " + score.ToString(); if (time < 5.0f) //時間制限 { time += Time.deltaTime; if (time >= 5.0f) { Debug.Log(A); } } ST = GameObject.Find("Stage"); scripta1 = ST.GetComponent<Stage2>(); T = scripta1.total; if (A == T) { sound.PlayBreak(); Debug.Log("O.K"); } else { sound.PlayMiss(); print("Mistake"); } }

scoreをシーンに表示してからsoundが鳴るように、時間制限を入れたのですが、コンパイルは通ったのですが、表示とsoundが同時で時間制限が働いていないようです。
誰か改善方法を教えてもらえませんか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

コルーチンで待つというのはどうでしょうか?

C#

1using UnityEngine; 2using System.Collections; 3using UnityEngine.UI; 4 5public class Check : MonoBehaviour 6{ 7 public GameObject IP; 8 public InputManager scripta; 9 public int A; 10 public GameObject ST; 11 public Stage2 scripta1; 12 public Text scoreText; 13 public int score = 0; 14 public int T; 15 private SoundManager sound; 16 public float time = 0.0f; 17 18 private float waitTime = 2.0f;//変更部分1 19 20 void Start() 21 { 22 scoreText.text = " "; 23 } 24 25 // Update is called once per frame 26 27 void Update() 28 { 29 if (Input.anyKey) 30 { 31 StartCoroutine(CheckCorutine());//コルーチンに変更 32 } 33 } 34 35 IEnumerator CheckCorutine() 36 { 37 sound = GameObject.Find("SoundManager").GetComponent<SoundManager>(); 38 39 IP = GameObject.Find("InputManager"); 40 scripta = IP.GetComponent<InputManager>(); 41 A = scripta.Answer; 42 score = A; 43 scoreText.text = " " + score.ToString(); 44 45 yield return new WaitForSeconds(waitTime);//変更部分2 46 /* 47 if (time < 5.0f) //時間制限 48 { 49 time += Time.deltaTime; 50 if (time >= 5.0f) 51 { 52 Debug.Log(A); 53 } 54 } 55 */ 56 ST = GameObject.Find("Stage"); 57 scripta1 = ST.GetComponent<Stage2>(); 58 T = scripta1.total; 59 if (A == T) 60 { 61 sound.PlayBreak(); 62 Debug.Log("O.K"); 63 } 64 else 65 { 66 sound.PlayMiss(); 67 print("Mistake"); 68 } 69 } 70}

上記はスコア表示の2秒後に音が鳴ると思うので待ち時間を変えたい場合は「private float waitTime = 2.0f;」の部分を変えて下さい。

投稿2017/10/04 11:31

編集2017/10/04 11:33
Hawn

総合スコア1222

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YukioMaki

2017/10/04 23:44

ha_waさん回答ありがとうございます。 スクリプトの変更まで書いていただき、初心者の私には本当に助かりました。 早速、支持の箇所を変更したらうまく作動しました。 これを機会にコールチンをもう1度勉強します。 これからもよろしくお願いします。
guest

0

if (time < 5.0f) に「時間制限」のコメントがあるのでもしかしたらそれの else で 後半の
ST = GameObject.Find("Stage"); 以降を囲んでやればやりたい事が実現できる?…のかな?

c#

1else { // ←これを挿入. 2 3 // ↓ここからは元々の処理↓. 4 ST = GameObject.Find("Stage"); 5 scripta1 = ST.GetComponent<Stage2>(); 6 T = scripta1.total; 7 if (A == T) 8 { 9 sound.PlayBreak(); 10 Debug.Log("O.K"); 11 } 12 else 13 { 14 sound.PlayMiss(); 15 print("Mistake"); 16 } 17 // ↑ここまでは元々の処理↑. 18 19} // ←これを挿入. 20

投稿2017/10/04 02:58

HiroshiWatanabe

総合スコア2160

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YukioMaki

2017/10/04 05:53

HiroshiWatanabeさん回答ありがとうございます。 ご指摘の場所にelseを挿入してみましたが、表示とsoundが同時で変わらないです。 他に問題点に気が付きましたら、また回答をお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問