ゲームの面を更新したいのと、ある一定の時間がきたらアクションを起こしたいのを
両方やりたいのですが詰まってしまったので教えて下さい。
具体的な例)
面をクリアするとPlayerPrefsの内容を変更してLoadSceneして新しい面を作成するInitScript.csと
時間が1分たつごとに音で知らせるTimerSound.csがあります。
InitScript.csとTimerSound.csは同じScene上のインスタンスで存在します。
しかし1分経つ前に面をクリアしてしまうとLoadSceneが発生するために
また最初に戻ってしまい、いつまで経っても音が出ません。
そこでこの問題を解決する方法を考えてみました。
1、LoadSceneをせずに新しい面を生成(更新)する方法があるか?
2、現状、InitScript.csとTimerSound.csは同じScene上のインスタンスとして
あるのですが例えばTimerSound.csのみSceneをの影響を受けない方法はできないか?
どちらかで解決するような気がするのですが具体的な方法が色々調べても分かりません。
分かる方教えて下さい。
環境)
PC: mac
Unity2018.3.14f1
言語:C#
InitScript.cs
public class InitScript : MonoBehaviour { private void NextGameInit() { imageFlag = PlayerPrefs.GetInt("imageflag", 0); if(imageFlag < 5) { imageFlag++; }else{ imageFlag = 0; } PlayerPrefs.SetInt("imageflag", imageFlag); // PlayerPrefs.SetInt("backcolorflag", backColorFlag); PlayerPrefs.SetInt("clearcount", 0); PlayerPrefs.SetInt("markflag", 0); PlayerPrefs.SetInt("checkid", 0); PlayerPrefs.Save(); SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single); } }
TimerSound.cs
public class TimerSound : MonoBehaviour { #pragma warning disable 649 [SerializeField]AudioClip pinpon; [SerializeField]AudioClip pipopiop; private float timeInterval = 60.0f; private float timeElapsed; private int timeCount; private AudioSource seSource; // Start is called before the first frame update void Start() { seSource = gameObject.GetComponent<AudioSource>(); timeCount = 0; } // Update is called once per frame void Update() { timeElapsed += Time.deltaTime; if(timeElapsed >= timeInterval) { if(timeCount < 2) { seSource.PlayOneShot(pinpon); timeCount++; }else{ seSource.PlayOneShot(pipopiop); timeCount = 0; } timeElapsed = 0.0f; } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/11 23:26