Right now I managed to make it work so that music keeps playing when I’m changing between scenes, but I have no idea how to make sure that it doesn’t load the music again when entering the same scene.
Right now I put the BackgroundMusic audio file with the script on MainMenu scene, and everything is working good, but when I go back to MainMenu scene via “Back to Main Menu” button, it starts the music again.
Can anyone tell me what am I supposed to do? Is there a single command line that I have to put in the button void to make sure it doesn’t start the music again?
Sorry for the late reply, i’ve been pretty busy!
So, for starting the music, there’s not really any code, I just made a empty game object, and gave it a sound component, and put in a music so it would play once the game has started and the window is open.
The Script is very simple, it’s just this
using UnityEngine;
using System.Collections;
public class AudioManager : MonoBehaviour {
public AudioSource BGM;
void Start () {
DontDestroyOnLoad (gameObject);
}
}
And then I placed this script inside the sound object so it would understand not to be destroyed once I change the scenes.
Add code to “FindGameObject” of your Audio Manager class in Awake of your Audio Manager script and destroy the new one if there’s already one (so you don’t have 2 of these ever running at the same time). Only “don’t destroy” after it’s been established that this is the only one left.
Add code to check the currently playing AudioClip in the Audio Source, if any (position > 0). If it is > 0, do nothing (exit method). If nothing playing (position = 0), play the clip. Turn off “start on awake” on the Audio Source. Make the code only do it.
I’m still learning Unity, so honestly I’ve no idea what you mean with both of those points.
Apologies for inconvenience. I am trying to understand how to do that, but no luck. Only getting random errors that unity doesn’t understand my code.
#1 is this, should happen in Awake method, which is the first thing that fires for a MonoBehavior script. Assuming your script class is “PlaySong”, it would be like this.
void Awake() {
if (FindObjectsByType(typeof(PlaySong)).Length > 1) {
Destroy(gameObject);
return; // don't allow code to continue executing since we're destroy this "extra" copy.
}
// put code for #2 here
}
#2 would be this:
var _audio = this.GetComponent<AudioSource>();
if (_audio.clip != null && _audio.time == 0) { // check if audio clip assigned and only do this if it hasn't started playing yet (position == 0)
_audio.Play();
}
This is all C#, I don’t do “UnityScript” or Javascript.
Thank you! I also use C#, so this is helping me out a lot!
Thank you for your help.
EDIT: So I tried to put these codes in, but it says that it can’t find “FindObjectsByType”, so I tried changing it to FindObjectOfType, and then there were no errors, but the game still starts a new song if I go back to main menu screen…
I’d go for something like this, attached to a Music Manager gameObject :
public class MusicPlayer {
static MusicPlayer instance = null;
void Awake () {
if (instance != null)
Destroy (gameObject);
else {
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
}
Note the use of Awake() rather than Start().
If you execute the code on Start(), you’ll get two instances of the player for a split second and you’ll hear some kind of glitch in the audio.
Yeah that’s more performant that FIndObject stuff, that takes care of #1 in my OP. Add a return statement after the Destroy call and then insert #2 code at the end of method above.