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

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

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

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

Q&A

解決済

1回答

8863閲覧

格ゲー風コマンド入力について

Cat_san

総合スコア19

Unity

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

0グッド

1クリップ

投稿2015/10/04 02:42

参考サイト:TokamakRocket 格ゲー風コマンド入力
http://tkmkrocket.net/doc/?p=88
http://tkmkrocket.net/doc/?p=95

サイトを参考にコマンド入力を確認するプログラムを組んでみたのですが以下のようなエラー文が表示されました。

「Assets/charControl.cs(12,14): error CS1624: The body of charControl.Start() cannot be an iterator block because `void' is not an iterator interface type」

イテレータの中にはvoid型は使えない、というエラー文だと思うのですが...どう解決すればいいのかわかりませんでした...

ご存知の方がいらっしゃいましたらご教授いただけましたら幸いです。
どうぞよろしくお願いいたします。

以下、作成したプログラムになります。

C#

1using UnityEngine; 2using System.Collections; 3using System.Text.RegularExpressions; 4 5public class charControl : MonoBehaviour { 6 7 string inputCommands = ""; 8 bool commandEnable = true; 9 int recCommandLength = 100; 10 11 // Use this for initialization 12 void Start () { 13 inputCommands = inputCommands.PadLeft(100); 14 StartCoroutine("confirmCommand"); 15 while (true){ 16 //command 17 if(commandEnable){ 18 getAxis(); 19 getFire(); 20 }else{ 21 inputCommands += " "; 22 } 23 yield return null; 24 }//end While 25 } 26 27 // Update is called once per frame 28 void Update () { 29 } 30 31 32 void getAxis(){ 33 if(Input.GetAxisRaw("Vertical") > 0.9){ 34 if(Input.GetAxisRaw("Horizontal") > 0.9){inputCommands += "9";} 35 else if (Input.GetAxisRaw("Horizontal") < -0.9){inputCommands += "7";} 36 else if (Input.GetAxisRaw("Horizontal") == 0){inputCommands += "8";} 37 else {inputCommands += "8";} 38 } 39 40 else if(Input.GetAxisRaw("Vertical") < -0.9){ 41 if(Input.GetAxisRaw("Horizontal") > 0.9){inputCommands += "3";} 42 else if(Input.GetAxisRaw("Horizontal") < -0.9){inputCommands += "1";} 43 else if(Input.GetAxisRaw("Horizontal") == 0){inputCommands += "2";} 44 else {} 45 } 46 else if(Input.GetAxisRaw("Vertical") == 0){ 47 48 if(Input.GetAxisRaw("Horizontal") > 0.9){inputCommands += "6";} 49 else if(Input.GetAxisRaw("Horizontal") < -0.9){inputCommands += "4";} 50 else if(Input.GetAxisRaw("Horizontal") == 0){inputCommands += "5";} 51 else {} 52 }else{ 53 54 } 55 56 if(inputCommands.Length > recCommandLength){ 57 inputCommands = inputCommands.Remove(0,1); 58 } 59 60 } 61 62 void getFire(){ 63 //fire 64 if(Input.GetButton("Fire1") == true){ 65 inputCommands += "f"; 66 } 67 68 if(inputCommands.Length > recCommandLength){ 69 inputCommands = inputCommands.Remove(0,1); 70 } 71 } 72 73 private IEnumerator confirmCommand(){ 74 while (true) { 75 int comLength = 0; 76 string checkframe = " "; 77 78 string testB = "6[0-9]*5[0-9]*2[0-9]*6[0-9]*f"; 79 comLength = 30; 80 checkframe = inputCommands.Remove (0, recCommandLength - comLength); 81 if (Regex.IsMatch (checkframe, testB)) { 82 Debug.Log ("SHORYUKEN!"); 83 yield return new WaitForSeconds (1.5f); 84 } 85 86 string testA = "2[0-9]*6[0-9]*[f]"; 87 comLength = 30; 88 checkframe = inputCommands.Remove (0, recCommandLength - comLength); 89 if (Regex.IsMatch (checkframe, testA)) { 90 Debug.Log("HADOUKEN!"); 91 yield return new WaitForSeconds (1.0f); 92 } 93 94 string testC = "f"; 95 comLength = 1; 96 checkframe = inputCommands.Remove (0, recCommandLength - comLength); 97 if (Regex.IsMatch (checkframe, testC)) { 98 Debug.Log("KOUGEKI!"); 99 yield return new WaitForSeconds (0.1f); 100 } 101 yield return null; 102 } 103 } 104}

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

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

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

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

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

guest

回答1

0

ベストアンサー

まず、エラー原因について読み間違いされています。
エラーメッセージで示されている行番号とそのメソッドを確認してみましょう。

csharp

1void Start () { 2 // 中略 3 yield return null; 4}

メソッドの返り値とreturnの値が一致していないのが原因です。

次にコルーチンの使い方を間違えています。
以下のサイトでコルーチンの使い方を詳しく説明されていますのでご覧ください。
その上で参考サイトのソースとご自分のStartメソッドをよく見比べてください。
Startメソッドに違いがあるでしょう?

Unityのコルーチンの使い方をまとめてみた

投稿2015/10/04 04:10

shiena

総合スコア1825

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

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

Cat_san

2015/10/04 16:05

>shienaさん リンクを貼っていただき、ありがとうございます。 無事動作しました。 エラー原因についてもう少し推測できるよう頑張りたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問