2016-08-02 11 views
0

サウンドを切り替えるためのUIボタンがあります。 OnClickイベントは、このシングルトンGameObjectにリンクされています。私が次のシーンに移動してメインシーンに戻ったとき、オブジェクトが階層内にまだ存在している間に、OnClickオブジェクトが見つからなくなりました。だから問題は何ですか ?別のシーンをロードした後にUIボタンが機能しなくなる

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class SoundsManagerController : MonoBehaviour { 

    static SoundsManagerController Instance = null;  



    void Awake() 
    { 
     // First we check if there are any other instances conflicting 
     if (Instance != null) 
     { 
            // If that is the case, we destroy other instances 
            Destroy(gameObject); 
     } 
     else { 
      // Here we save our singleton instance 
      Instance = this; 

      // Furthermore we make sure that we don't destroy between scenes (this is optional) 
      DontDestroyOnLoad(gameObject); 
     } 
    } 

    public void toggleSound(){ 

     Instance.GetComponent<AudioSource>().enabled = !Instance.GetComponent< AudioSource>().enabled; 
    } 

} 

答えて

0

ボタンに別のキャンバスを使用し、シングルトンオブジェクトの子として保持します。

シーンにキャンバスがある場合は、ボタンがキャンバスの子として設定されていることを確認してください。

これらが問題を解決する必要がある場合は、シングルトンオブジェクトでボタンを使用しないことを検討する必要があります。任意のシーンでシングルトンオブジェクトをStart()に取得し、UIレイアウトを維持している既に配置されたボタンからサウンドトグルにアクセスします。

開始時にトグルメソッド呼び出し関数を保持しているシングルトンオブジェクトを取得します。

SoundManagerController soundManager; 

void Start() 
{ 
soundManager = GameObject.FindWithTag("audio_manager_tag").GetComponent <SoundManagerController>(); 
} 

トグルボタンから以下のメソッドを呼び出します。

public void CallToggleMethod() 
{ 
soundManager.toggleSound(); 
} 
+0

あなたの助けに感謝..しかし、私は、複数のキャンバスを持つとUIのボタンはトグルのために配置されるシングルトンオブジェクト – dotfreelancer

+0

の彼らの子供を作る好きではないので、あなたはより良いアプローチのリンクや例を提供することができますか? –

+0

キャンバスの下に – dotfreelancer

関連する問題