You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 5.3中DontDestroyOnLoad导致主场景音频失效问题

Fixing DontDestroyOnLoad Audio Issues in Unity 5.3

Let's break down what's happening here and how to fix it.

First, the core problem: you have an AudioManager in both your menu and main scenes. When you switch from the menu to the main scene, the DontDestroyOnLoad call keeps the menu's AudioManager alive, and the main scene's AudioManager gets destroyed (thanks to your Awake logic). But here's the catch—if your main scene's scripts are trying to play sounds via AudioManager.instance.PlaySound(), they're using the menu's AudioManager instance, which doesn't have the main scene's sound entries in its sounds array. That's why you likely see the "Sound not found" warning (check your console!) and no manager-controlled sounds play, while manually placed AudioSources work fine (they're part of the main scene and don't rely on the global instance).

Here are two solid solutions tailored for Unity 5.3:

This is the cleanest approach for most cases:

  • Remove the AudioManager from your main scene entirely—only keep it in the menu scene.
  • Open the menu scene's AudioManager, and add all sounds you need across both scenes (menu sounds + main scene sounds) to its sounds array in the Inspector.
  • Now, when you switch scenes, the menu's AudioManager stays alive, and every call to AudioManager.instance.PlaySound("SoundName") will find the correct sound entry, no matter which scene you're in.

Solution 2: Dynamically Merge Scene Sounds to Global Instance

If you prefer keeping scene-specific sound configurations separate (e.g., each scene's AudioManager defines its own sounds), modify your AudioManager code to merge new scene sounds into the global instance instead of destroying the new manager:

Update your Awake method and add a merge helper function:

void Awake() {
    if (instance != null) {
        if (instance != this) {
            // Merge current scene's sounds into the global instance
            MergeSceneSoundsToGlobal();
            // Destroy this scene's redundant AudioManager
            Destroy(gameObject);
        }
    } else {
        instance = this;
        DontDestroyOnLoad(this);
    }
}

private void MergeSceneSoundsToGlobal() {
    foreach (Sound newSceneSound in sounds) {
        // Skip duplicate sounds to avoid conflicts
        bool soundExists = false;
        foreach (Sound globalSound in instance.sounds) {
            if (globalSound.name == newSceneSound.name) {
                soundExists = true;
                break;
            }
        }

        if (!soundExists) {
            // Expand the global sounds array and add the new sound
            System.Array.Resize(ref instance.sounds, instance.sounds.Length + 1);
            instance.sounds[instance.sounds.Length - 1] = newSceneSound;

            // Create an AudioSource for the new sound under the global manager
            GameObject soundObject = new GameObject($"Sound_{instance.sounds.Length - 1}_{newSceneSound.name}");
            soundObject.transform.SetParent(instance.transform);
            newSceneSound.SetSource(soundObject.AddComponent<AudioSource>());
        }
    }
}

Also, update your Start method to only initialize sounds if this is the global instance (prevents duplicate initialization):

void Start() {
    if (instance == this) {
        for (int i = 0; i < sounds.Length; i++) {
            GameObject soundObject = new GameObject($"Sound_{i}_{sounds[i].name}");
            soundObject.transform.SetParent(transform);
            sounds[i].SetSource(soundObject.AddComponent<AudioSource>());
        }
        PlaySound("Music");
    }
}

Unity 5.3 Specific Notes

  • Double-check that all sound clips are included in your build (go to File > Build Settings > Player Settings > Other Settings and ensure your audio assets are marked as Streaming or Compressed as needed).
  • In Unity 5.3, DontDestroyOnLoad works reliably for root-level game objects, so keep your AudioManager as a root object to avoid any hierarchy-related issues.
  • Test in the editor first: switch scenes and check the Console for "Sound not found" warnings—this will confirm if the missing sound entries are the root cause.

内容的提问来源于stack exchange,提问作者Chi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:04:41