前提・実現したいこと
If文を使って実行するコルーチンを選びたい。
発生している問題・エラーメッセージ
IsPlayerTurnがTrueのとき、PlayerAttackTurnというコルーチンを実行し、 IsPlayerTurnがfalseのとき、Enemyというコルーチン実行をして、 それを繰り返すことで、ターン制バトルのテキスト表記を変化させたいのですが、 下記のスクリプトだとコルーチンが実行されないです。 書き方が間違っているのか、そもそもコルーチンの選択はできないのか知りたいです。 また最初にStartsというコルーチンを実行させたいです。 ご意見よろしくお願いします。
該当のソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TalkSystem : MonoBehaviour { GameObject TalkText; Text Tt; GameObject ButtonSystem; GameObject EnemyBattleSystem; bool IsPlayerTurn; bool IsRunning = false; public int UIonoff; public int Branch; public int EAturn; public int EAttack; public int EAStrat; void Start() { TalkText = GameObject.Find("TalkText"); Tt = TalkText.GetComponentInChildren<Text>(); ButtonSystem = GameObject.Find("ButtonSystem"); Branch = ButtonSystem.GetComponent<ButtonSystem>().Branch; EnemyBattleSystem = GameObject.Find("EnemyBattleSystem"); EAturn = EnemyBattleSystem.GetComponent<EnemyBattleSystem>().EAturn; EAttack = EnemyBattleSystem.GetComponent<EnemyBattleSystem>().EAttack; IsPlayerTurn = true; StartCoroutine("Starts",1); } // Start is called before the first frame update IEnumerable Starts() { Tt.text = "???が表れた!"; yield return new WaitUntil(() => Input.GetKeyDown("return")); yield return null; } IEnumerator EnemyTalk() { Tt.text = "敵のターンだ!"; yield return new WaitUntil(() => Input.GetKeyDown("return")); yield return null; Tt.text = "敵の攻撃だ!"; yield return new WaitUntil(() => Input.GetKeyDown("return")); yield return null; EAStrat = 1; if (EAturn == 1) { Tt.text = "あなたは" + EAttack.ToString() + "のダメージを受けた!"; } yield return new WaitUntil(() => Input.GetKeyDown("return")); yield return null; EAturn = 0; EAStrat = 0; IsPlayerTurn = true; } IEnumerator PlayerTalk() { Tt.text = "あなたのターンだ!"; yield return new WaitUntil(() => Input.GetKeyDown("return")); yield return null; Tt.text = "行動を選択せよ"; UIonoff = 1; if (Branch == 0) { Tt.text = "敵は10のダメージを受けた!"; UIonoff = 0; } else if (Branch == 1) { Tt.text = "敵は50のダメージを受けた!"; UIonoff = 0; } else if (Branch == 2) { Tt.text = "あなたは10回復した!"; UIonoff = 0; } yield return new WaitUntil(() => Input.GetKeyDown("return")); yield return null; IsPlayerTurn = false; } // Update is called once per frame void Update() { if (!IsPlayerTurn) { StartCoroutine("Enemy",10); } if (IsPlayerTurn) { StartCoroutine("PlayerAttackTurn",10); } } }
補足情報(FW/ツールのバージョンなど)
UnityVersion 2019.1.4f1 Personal
StartCoroutine の引数と同じ名前のメソッドがありません。また、Update 内で毎回 StartCoroutine を呼んでいますが、これは一度だけで良いはずです。フラグによって二つのコルーチンを呼びわけようとしていますが、コルーチン内で分岐するのが良いと思います。
あなたの回答
tips
プレビュー