実現したいこと
・別のスクリプトの変数を参照し、そこに格納されている文章を画面に表示する。(完了)
・その際3秒ごとに実行したい(ここが解決できません)
前提
kanri.csというスクリプトのsample_textの文章を取得し,text.csでunity上に表示する.kanri.csのvoid update内でテキストの内容を変化させながら,画面表示させることまではできました.
1度表示させるごとに3秒待機するよう改良を加えたいと思い,調べるとIEnumeratorというものがあったため活用,しかしエラーが発生し解決できませんでした.
プログラミング初学者のため非常につたない質問で申し訳ありません よろしくお願いいたします
発生している問題・エラーメッセージ
not all code paths return a value
該当のソースコード
kanri.cs
C#
1using System.Collections;2using System.Collections.Generic;3using UnityEngine;4 5public class kanri : MonoBehaviour 6{7 //ほかのスクリプトからアクセス可能な変数8 public string sample_text;9 public int x = 1;10 //ほかのスクリプトからアクセス可能なメソッド11 void Start()12 {13 14 StartCoroutine("ChangeColor");15 16 }17 18 // void Update()19 // {20 // sample_text = "" + x;21 // if (x > 9)22 // {23 // sample_text = "ohayo!";24 // x = 0;25 // }26 27 28 // x = x + 1;29 // }30 31 IEnumerator ChangeColor()32 {33 34 35 sample_text = "ohayo"+ x;36 3秒停止 37 yield return new WaitForSeconds(3);38 x += 1;39 40 }41}
text.cs
C#
1using System.Collections;2using System.Collections.Generic;3using UnityEngine;4using UnityEngine.UI;5 6public class text : MonoBehaviour 7{8 9 public GameObject score_object = null; // Textオブジェクト10 kanri script;11 12 // 初期化13 void Start()14 {15 16 }17 18 // 更新19 void Update()20 {21 // オブジェクトからTextコンポーネントを取得22 Text score_text = score_object.GetComponent<Text>();23 script = GameObject.Find("GameObject").GetComponent<kanri>();24 score_text.text = script.sample_text;25 26 27 28 }29}
0 コメント