var bgMusicItem = GameObject.GetComponent<AudioSource>() ?? GameObject.AddComponent<AudioSource>();
Debug.Log(bgMusicItem.isPlaying);
Sometimes the code above may generate the following error:
MissingComponentException: There is no ‘AudioSource’ attached to the “SoundManager” game object, but a script is trying to access it.You probably need to add a AudioSource to the game object “SoundManager”. Or your script needs to check if the component is attached before using it.
Does anyone know why this happens?Any insight or information would be greatly appreciated.