初めまして。Unityでゲームを作りたいと思って勉強中のものです。
お手間かと思いますが、解決の手助けをいただけますと助かります。よろしくお願いいたします。
■前提・実現したいこと
ボタンが押されたら画像を表示して〇秒後に見えなくする。もう一度ボタンが押されても同じように画像が表示されて〇秒後に見えなくしたい。
■発生している問題・エラーメッセージ
〇秒後に見えなくなったが、再度ボタンを押しても何も反応しない。
■該当のソースコード
C#
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine.UI; 4 using UnityEngine; 5 6 7public class ChangeImage : MonoBehaviour 8 { 9 public GameObject Buttongohan; // GameObject"Button(gohan)"を定義 10 public GameObject Buttonasobu; // GameObject"Button(asobu)"を定義 11 12 GameObject memoryGohan; 13 GameObject memoryAsobu; 14 15 int countGohan; 16 int countAsobu; 17 18 bool onOff = false; 19 20 void Start() 21 { 22 memoryGohan = Buttongohan; 23 memoryAsobu = Buttonasobu; 24 25 countGohan = 0; 26 countAsobu = 0; 27 28 memoryGohan.SetActive(onOff); 29 memoryAsobu.SetActive(onOff); 30 } 31 32 public void Button_gohan() 33 { 34 if (countGohan == 0) 35 { 36 Debug.Log("押された!"); // ログを出力 37 onOff = false; 38 memoryAsobu.SetActive(onOff); 39 countAsobu = 0; 40 onOff = true; 41 memoryGohan.SetActive(onOff); 42 countGohan++; 43 } 44 45 46 Debug.Log("botan押された!"); // ログを出力 47 48 StartCoroutine( Uniwait() ); 49 50 IEnumerator Uniwait() 51 { 52 onOff = true; 53 memoryGohan.SetActive(onOff); 54 55 Debug.Log("3秒まつ"); 56 yield return new WaitForSeconds(3.0f); 57 58 onOff = false; 59 60 memoryGohan.SetActive(onOff); 61 } 62 }
■自分で調べたことや試したこと
はじめは秒数を数えるため「Time.deltaTime」を使いましたが、update内で完結する方法が分からず、上記にたどり着きました。
C#
1 void Start() 2 { 3 time = 3; 4 Debug.Log("スタート!"); // ログを出力 5 uni_eat.SetActive(true); 6 } 7 8 void Update() 9 { 10 Debug.Log("タイム減るよ"); // ログを出力 11 time -= Time.deltaTime; 12 13 if (time <= 0.0f) 14 { 15 uni_eat.SetActive(false); 16 }
■使っているツールのバージョンなど補足情報
Unityのバージョン:Unity 2021.3.2f1
Windows
貼り付けたコードを自分で読み返しておかしいことに気づくと思います。markdown を正しく指定して読めるようにしてください。
https://qiita.com/kai0310/items/d6ad23d7e60eeb103c9d