{"id":5921,"date":"2025-03-25T16:34:48","date_gmt":"2025-03-25T07:34:48","guid":{"rendered":"https:\/\/blog.criware.com\/?p=5921"},"modified":"2025-08-06T17:32:16","modified_gmt":"2025-08-06T08:32:16","slug":"playing-sounds-between-scenes","status":"publish","type":"post","link":"https:\/\/blog.criware.com\/index.php\/2025\/03\/25\/playing-sounds-between-scenes\/","title":{"rendered":"Playing sounds between scenes"},"content":{"rendered":"<p>In adventure games, the heroes explore many different locations. For instance, while walking in the forest, they could emerge in a magical clearing.\u00a0 In horror games, it is common for a door to slowly open and make a creaking sound as the player moves into the next room.<\/p>\n<p>However, if the clearing or the next room are designed as different scenes in Unity, the audio object assuring the playback will be released when switching to the next scene, and the audio continuity will be lost. In the case of the creaking door, the sound will be cut off midway!<\/p>\n<p>In this article, we will demonstrate how we can keep that creepy door sound playing without interruption. <\/p>\n<div style=\"max-width: 700px; margin: 0 auto;\">\n<div style=\"width: 800px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-5921-1\" width=\"800\" height=\"419\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2025\/03\/blog-video.mp4?_=1\" \/><a href=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2025\/03\/blog-video.mp4\">https:\/\/blog.criware.com\/wp-content\/uploads\/2025\/03\/blog-video.mp4<\/a><\/video><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Basically, this is achieved by selecting the &#8220;Don&#8217;t Destroy On Load&#8221; option for sound playback objects such as the CRIWARE Library Initializer and CRIWARE Error Handler, prefabricating them, and using them in multiple game scenes.<\/p>\n<p>In this example, we created a DoorSoundManager singleton to help us keep the door sound playing between the rooms\/scenes.<\/p>\n<p>Here are the details:<\/p>\n<h2 style=\"font-size: 18px; padding-left: 10px; border-left: 6px solid #000;\">Singleton implementation<\/h2>\n<ul>\n<li>The DoorSound class is designed as a singleton and can be accessed from anywhere via the Instance property.<\/li>\n<li>The Awake method manages instances and destroys duplicate objects.<\/li>\n<\/ul>\n<h2 style=\"font-size: 18px; padding-left: 10px; border-left: 6px solid #000;\">CriAtomSource component<\/h2>\n<ul>\n<li>The AttachCriAtomSource method dynamically adds a CriAtomSource component and sets the CueSheet and Cue names.<\/li>\n<\/ul>\n<h2 style=\"font-size: 18px; padding-left: 10px; border-left: 6px solid #000;\">Sound playback<\/h2>\n<ul>\n<li>The PlayDoorSound method can be used to play the sound with the specific CueSheet and Cue names.<\/li>\n<\/ul>\n<pre style=\"width: 94%; margin-bottom: 0px; padding-bottom: 40px;\"><code class=\"hljs php\">\r\nusing CriWare;\r\nusing UnityEngine;\r\n\r\npublic class DoorSound : MonoBehaviour\r\n{\r\n    \/\/ Singleton instance\r\n    public static DoorSound Instance { get; private set; }\r\n\r\n    private CriAtomSource criAtomSource;\r\n\r\n    void Awake()\r\n    {\r\n        \/\/ Initialize singleton\r\n        if (Instance == null)\r\n        {\r\n            Instance = this;\r\n            DontDestroyOnLoad(gameObject); \/\/ Make the object persistent\r\n        }\r\n        else\r\n        {\r\n            Destroy(gameObject); \/\/ Destroy duplicate instance\r\n            return; \/\/ Do not execute further\r\n        }\r\n\r\n        \/\/ Dynamically attach CriAtomSource\r\n        AttachCriAtomSource();\r\n    }\r\n\r\n    private void AttachCriAtomSource()\r\n    {\r\n        \/\/ Add CriAtomSource component\r\n        criAtomSource = gameObject.AddComponent<CriAtomSource>();\r\n\r\n        if (criAtomSource != null)\r\n        {\r\n            Debug.Log(\"CriAtomSource has been successfully attached.\");\r\n\r\n            \/\/ Set default settings if needed (e.g., specify CueSheet name)\r\n            criAtomSource.cueSheet = \"\";\r\n            criAtomSource.cueName = \"\";\r\n        }\r\n        else\r\n        {\r\n            Debug.LogError(\"Failed to attach CriAtomSource!\");\r\n        }\r\n    }\r\n\r\n    public void PlayDoorSound(string cueSheet, string cueName)\r\n    {\r\n        if (criAtomSource != null)\r\n        {\r\n            \/\/ Set CueSheet\r\n            criAtomSource.cueSheet = cueSheet;\r\n\r\n            \/\/ Play with the specified CueName\r\n            criAtomSource.Play(cueName);\r\n        }\r\n        else\r\n        {\r\n            Debug.LogError(\"CriAtomSource is not set.\");\r\n        }\r\n    }\r\n}\r\n\r\n<\/code><\/pre>\n<p>As for the door itself, here is how it is handled:<\/p>\n<ul>\n<li>We gave the door object an opening animation<\/li>\n<li>When you click on the door on the game screen, the sound playback function of the DoorSoundManager singleton is called, and after one second, the game transitions to the next room\/scene.<\/li>\n<\/ul>\n<p>Below is the part of the audio code attached to the door object:<\/p>\n<pre style=\"width: 94%; margin-bottom: 0px; padding-bottom: 40px;\"><code class=\"hljs php\">\r\n\/\/ cueSheet and cueName in the Inspector\r\npublic string cueSheet;\r\npublic string cueName;\r\n\r\n\u2026\u2026\r\n\r\n\/\/ calling the DoorSound playback\r\nDoorSound.Instance.PlayDoorSound(cueSheet, cueName);\r\n<\/code><\/pre>\n<p>By calling DoorSound.Instance.PlayDoorSound, the game object creates the singleton that can be accessed from any scene, and all that is left to do is to pass the right CueSheet names and Cue names to the PlayDoorSound function. For example, you could prepare several sounds for doors of various sizes and materials, and select the right one based on the player\u2019s location.<\/p>\n<p>To learn more about maintaining sound continuity when switching between scenes, we recommend referring to these two pages of the official documentation for further details:<\/p>\n<p><a href=\"https:\/\/game.criware.jp\/manual\/unity_plugin_en\/latest\/contents\/cri4u_samples_criatom_script_over_scene_sound.html\">https:\/\/game.criware.jp\/manual\/unity_plugin_en\/latest\/contents\/cri4u_samples_criatom_script_over_scene_sound.html<\/a><\/p>\n<p><a href=\"https:\/\/game.criware.jp\/manual\/unity_plugin_en\/latest\/contents\/cri4u_samples_criatom_script_over_scene_everywhere.html\">https:\/\/game.criware.jp\/manual\/unity_plugin_en\/latest\/contents\/cri4u_samples_criatom_script_over_scene_everywhere.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In adventure games, the heroes explore many different locations. For instance, while walking in the forest, they could emerge in<\/p>\n","protected":false},"author":2,"featured_media":5925,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[5,7,23],"tags":[],"class_list":["post-5921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adx","category-tutorials","category-unity"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/5921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/comments?post=5921"}],"version-history":[{"count":12,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/5921\/revisions"}],"predecessor-version":[{"id":5935,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/5921\/revisions\/5935"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/media\/5925"}],"wp:attachment":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/media?parent=5921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/categories?post=5921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/tags?post=5921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}