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;
}
}
あなたの助けに感謝..しかし、私は、複数のキャンバスを持つとUIのボタンはトグルのために配置されるシングルトンオブジェクト – dotfreelancer
の彼らの子供を作る好きではないので、あなたはより良いアプローチのリンクや例を提供することができますか? –
キャンバスの下に – dotfreelancer