I am coding a C# script in Unity. I code audio playing in multiple scenes and it doesn't stop playing in another scene I expected.
This is the scene I start playing the audio script BgScript.cs.
This code is used for playing and stopping audio.
BgScript.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BgScript : MonoBehaviour { private static BgScript instance = null; public static BgScript Instance { get { return instance; } } void Awake() { if (instance != null && instance != this) { Destroy(this.gameObject); return; } else { instance = this; } DontDestroyOnLoad(this.gameObject); } }
This is the image of where I place a script AudioPause.cs for audio stopping. enter image description here
And this is my audio stopping code
AudioPause.cs.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AudioPause : MonoBehaviour { void start() { BgScript.Instance.gameObject.GetComponent<AudioSource>().Stop(); } }
start() is lowercased in AudioPause.cs and thus the code never runs because it is never called.
void Start() { }
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html
1.4m articles
1.4m replys
5 comments
57.0k users