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

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

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

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

Q&A

解決済

1回答

924閲覧

スクリプトから、別のスクリプトのメソッドを実行したときの、コルーチンの実行について

AnimatorMasatoy

総合スコア45

Unity

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

0グッド

0クリップ

投稿2021/11/09 15:51

編集2021/11/10 13:57

Unityで、スクリプトから他のスクリプトのメソッドを実行し、そのメソッドのコルーチンを実行するとなぜかそのオブジェクトは非アクティブなので実行できないと返されます。参照されたスクリプトは非アクティブではないです。

Coroutine couldn't be started because the the game object 'Canvas' is inactive!

Canvas => 参照されたスクリプト

https://teratail.com/questions/342876
こちらの似たような症状が起きている投稿を確認しました。
返信欄を見て、「宣言したスクリプトをnewにするのではなくアタッチしたオブジェクトを参照するようにしてください。」
newにすると、NullReferenceExceptionのエラーを返すそうですが(実際に試しました)オブジェクトをインスペクターからアタッチし、実行をすると非アクティブのエラーを返します。

具体的な対処法も原因もわかりません...

PlayerController

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class PlayerController : MonoBehaviour 7{ 8 9 public GameObject UICanvas; 10 11 public static bool IsAFK = false; 12 int AFKTimer = 0; 13 14 // Start is called before the first frame update 15 void Start() 16 {} 17 void FixedUpdate() 18 { 19 20 if(!IsAFK && !Input.GetButtonDown("Jump")){ 21 AFKTimer++; 22 } 23 //Debug.Log(AFKTimer); 24 if(AFKTimer > 60){ 25 IsAFK = true; 26 AFKTimer = 0; 27 Debug.Log("放置"); 28 GameManager uic = UICanvas.GetComponent<GameManager>(); 29 uic.AFKBarEffect(1); //<-----コルーチン実行失敗 30 } 31 32 if(Input.GetButtonDown("Jump")){ 33 if(IsAFK){ 34 IsAFK = false; 35 Debug.Log("離脱"); 36 GameManager uic = UICanvas.GetComponent<GameManager>(); 37 uic.AFKBarEffect(2); 38 } 39 else{AFKTimer = 0;} 40 } 41 } 42} 43

GameManager

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class GameManager : MonoBehaviour 7{ 8 //プレイヤー操作 9 10 public GameObject Player; 11 12 // Start is called before the first frame update 13 void Start() 14 { 15 Player = GameObject.FindGameObjectWithTag("Player"); 16 } 17 18 // Update is called once per frame 19 void Update() 20 {} 21 22 public void AFKBarEffect(int mode){ 23 if(mode == 1){ 24 StartCoroutine( CorAFK() ); 25 } 26 else{ 27 StartCoroutine( CorBack() ); 28 } 29 IEnumerator CorAFK(){ 30 Debug.Log("コルーチン実行"); 31 yield return new WaitForSeconds(0.1f); 32 Debug.Log("コルーチン実行"); 33 } 34 IEnumerator CorBack(){ 35 Debug.Log("コルーチン実行"); 36 yield return new WaitForSeconds(0.1f); 37 Debug.Log("コルーチン実行"); 38 } 39 } 40}

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

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

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

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

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

y_waiwai

2021/11/09 22:59

コードを提示しよう
guest

回答1

0

自己解決

もう一度組みなおしたら正しく動作しました

投稿2022/01/13 09:20

AnimatorMasatoy

総合スコア45

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問