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

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

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

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1234閲覧

コルーチンがうまく動作しない

ichigo_0308

総合スコア41

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/07/04 03:41

<実現したいこと>

ロングタップしてタップした秒数(nowTapTime)がロングタップしたと判定する変数(longTapTime)を超えた&1秒待って if ((longTapTime - safeTime < nowTapTime)&& (longTapTime + safeTime > nowTapTime))(safeTimeはlongTapTimeから何秒離れていいかを示す変数です。)がtrueならロングタップ成功フラグが立つようにしているのですが、(太字のところはコルーチンです。)コルーチンがうまく反応しません。

<エラーコード>

Coroutine'WAitASecond' couldn't be started!

<ソースコード>

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class GameScript : MonoBehaviour 7{ 8 public float longTapTime=1.0f; 9 float nowTapTime; 10 float safeTime; 11 bool isLongtap; 12 bool isLongtapclear; 13 int level; 14 public Text countText; 15 public string count; 16 bool startflag=false; 17 // Start is called before the first frame update 18 void Start() 19 { 20 level = PlayerPrefs.GetInt("level"); 21 count3(); 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 if (startflag) 28 { 29 if (Input.GetMouseButton(0)) 30 { 31 nowTapTime += Time.deltaTime; // 秒数をカウント 32 33 // タップし続けた時間が規定値を超えたらロングタップとして扱う 34 if (nowTapTime >= longTapTime && !isLongtap) 35 { 36 StartCoroutine("WaitASecond"); 37 nowTapTime = 0; // タイマーリセット 38 isLongtap = true; // ロングタップしたフラグを立てる 39 Debug.Log("Long Tap"); 40 } 41 42 } 43 else if (Input.GetMouseButtonUp(0)) 44 { 45 // クリックを終えたら初期化 46 nowTapTime = 0; 47 isLongtap = false; 48 } 49 } 50 } 51 public void kabe() 52 { 53 safeTime = 1 / (level + 1); 54 safeTime/=0.9f; 55 } 56 public void count3() 57 { 58 count = "3"; 59 countText.text = count; 60 Invoke("count2", 1); 61 } 62 public void count2() 63 { 64 count = "2"; 65 countText.text = count; 66 Invoke("count1", 1); 67 } 68 public void count1() 69 { 70 count = "1"; 71 countText.text = count; 72 Invoke("start", 1); 73 } 74 public void start() 75 { 76 count = "start!"; 77 countText.text = count; 78 Invoke("startsetting", 1); 79 } 80 public void startsetting() 81 { 82 startflag = true; 83 84 } 85 IEnumerator WaitASecond() 86 { 87 yield return new WaitForSeconds(1f); 88 float diff = longTapTime - nowTapTime; 89 if(diff<0) 90 { 91 diff *= -1; 92 } 93 if(diff<safeTime) 94 { 95 96 } 97 if ((longTapTime - safeTime < nowTapTime) 98 && (longTapTime + safeTime > nowTapTime)&&!isLongtapclear) 99 { 100 isLongtapclear = true; 101 } 102 } 103}

<試したこと>

名前が違っていたので、直しましたがまだエラーが出ます。

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

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

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

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

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

guest

回答1

0

自己解決

ただプログラムを保存していなかっただけでした すみません!

投稿2020/07/04 03:44

ichigo_0308

総合スコア41

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問