プログラミング初心者です。
現在、タイトル画面からオプション画面にシーンごと移動して、音量の調整を行えるようにしたいです。
オプション画面のみであれば、音量の調整はできますが、シーンを移動するとできなくなってしまいます。
破壊された audioSourceBGMを読む込み、読み取れていないのかなと思います。破壊される前の一番最初のオブジェクトを読めるようにしたいです。(推察です。)
下にコードを乗せています。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Titol : MonoBehaviour
{
private AudioSource audioSourceSE;
private AudioSource audioSourceBGM;
public AudioClip se;
public AudioClip bgm;
public Slider seSlider; public Slider bgmSlider; public static Titol Instance; private void Awake() { audioSourceSE = gameObject.AddComponent<AudioSource>(); audioSourceBGM = gameObject.AddComponent<AudioSource>(); audioSourceBGM.loop = true; audioSourceBGM.clip = bgm; audioSourceBGM.Play(); // 初回のみInstanceをセット if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(audioSourceBGM); // 既存のBGMを停止 Instance = this; // 新しいオブジェクトをInstanceにセット DontDestroyOnLoad(gameObject); } // シーンがロードされたときに呼び出されるデリゲートを追加 SceneManager.sceneLoaded += OnSceneLoaded; // スライダーの初期値を設定 seSlider.value = audioSourceSE.volume; bgmSlider.value = audioSourceBGM.volume; } public void SettingPlaySE() { audioSourceSE.PlayOneShot(se); } public void StartBottan() { SceneManager.LoadScene("GameSelect"); SettingPlaySE(); } public void SetumeiBottan() { SceneManager.LoadScene("setumei"); SettingPlaySE(); } public void GmeiBottan() { SceneManager.LoadScene("gyarari-"); SettingPlaySE(); } public void OmeiBottan() { SceneManager.LoadScene("opputyon"); SettingPlaySE(); } public void AmeitBottan() { SceneManager.LoadScene("titl"); SettingPlaySE(); } // 修正: OnLevelWasLoadedを非推奨から変更 private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { // シーンが切り替わるたびにBGMを再生 if (Instance != null) { Instance.PlayBGM(); } } // 修正: PlayBGMを非staticメソッドに変更 public void PlayBGM() { if (!audioSourceBGM.isPlaying) { audioSourceBGM.Play(); } } public void OnSEVolumeChanged(float volume) { audioSourceSE.volume = volume; } // BGMの音量をスライダーから変更 public void OnBGMVolumeChanged(float volume) { audioSourceBGM.volume = volume; }
}
シーン移動して、スライダーをいじると。
エラーはこのように出ます。
MissingReferenceException: The object of type 'AudioSource' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/11/23 10:12
2023/11/23 10:34
2023/11/23 10:45
2023/11/23 10:49
2023/11/23 11:33
2023/11/23 11:53
2023/11/23 11:55
2023/11/23 12:00
2023/11/23 12:54
2023/11/23 14:15
2023/11/23 14:15
2023/11/23 14:17
2023/11/24 02:39
2023/11/24 14:10
2023/11/24 18:25
2023/11/24 21:07
2023/11/25 02:49
2023/11/25 03:53
2023/11/25 04:04
2023/11/25 04:57
2023/11/25 05:14
2023/11/25 14:29
2023/11/25 19:43
2023/11/26 02:33 編集
2023/11/26 02:32
2023/11/26 02:40
2023/11/26 02:49
2023/11/26 02:59
2023/11/26 03:40
2023/11/26 16:19
2023/11/27 02:20
2023/11/27 02:34
2023/11/27 02:40
2023/11/27 02:44
2023/11/27 02:44
2023/11/27 02:46
2023/11/27 02:58
2023/11/27 03:08
2023/11/27 09:58