Problem with adding new options to MusicPlayer

I wanted to have an option to choose from 5 music tracks played in background. And mainly I did this. Here’s what’s going on:

  1. Music starts playing when game is started
  2. When I go to options screen, where I choose music track, it stops playing
  3. I choose different track (I’m still in options screen), and it’s silent still
  4. When I go back to start screen it plays this different track, and it’s played throughout the game

Here are the scripts:

MusicPlayer.cs - declared variables

public static int musicTrackInt = 0;
public static string[] musicTracks = {"1", "2", "3", "4", "5"};

public AudioClip bgMusic1;
public AudioClip bgMusic2;
public AudioClip bgMusic3;
public AudioClip bgMusic4;
public AudioClip bgMusic5;

public AudioSource bgMusic;

There are music files attached to audioclips in inspector.

MusicPlayer.cs - Start function

void Start () 
{
	StartAudioVolumes(); //irrelevant in this case
	bgMusic = GetComponent<AudioSource>();
	bgMusic.clip = bgMusic1;
	bgMusic.Play();
}

MusicPlayer.cs - MusicTrackSelection function

public void MusicTrackSelection() 
{
	switch(musicTrackInt)
	{
		case 0:
			bgMusic.clip = bgMusic1;
			bgMusic.Play();
			break;
		case 1:
			bgMusic.clip = bgMusic2;
			bgMusic.Play();
			break;
		case 2:
			bgMusic.clip = bgMusic3;
			bgMusic.Play();
			break;
		case 3:
			bgMusic.clip = bgMusic4;
			bgMusic.Play();
			break;
		case 4:
			bgMusic.clip = bgMusic5;
			bgMusic.Play();
			break;			
	}
	
}

OptionsScreen.cs - declared variables

private MusicPlayer musicPlayer;

OptionsScreen.cs - Start function

void Start () 
{
	musicPlayer = GameObject.FindObjectOfType<MusicPlayer>();
	OptionsArea (); //irrelevant in this case
}

OptionsScreen.cs - OnGui function

MusicPlayer.musicTrackInt = GUI.Toolbar (new Rect(musicTrackToolbar), MusicPlayer.musicTrackInt, MusicPlayer.musicTracks, "Options Toolbars");
musicPlayer.MusicTrackSelection();

I don’t know why music stops playing when on options screen. I must have missed something, I suppose it’s a logic mistake. Thanks in advance.

The game object / what object handles playing of music, are you sure that is in the Scene for your Options Screen?

Thats the first thing that came to my head when reading your post.

Edit**
Actually, just wanted to check, any error messages being logged out of the console?

Also, for your switch statement, add a default one after case 4:, in case the code isn’t picking any of the options / bgMusic you want it to check for. Maybe have it Debug.Log out ( “Couldn’t find music bg to play?”) ;

No error messages. And to the default statement - it’s not needed, because there’s 0 at start of the game, so when I launch Options screen musicTrackInt is 0. I logged it out. When I choose any other track, log also shows it correctly which one is it. I tried with default before, effect was the same. And finally to MusicPlayer in OptionsScreen scene, I will check it when have acces to laptop, but as I remember it was ok. MusicPlayer as GameObject is only on StartScreen, but don’t destroying on load, so it’s on every scene, on Options too. But I’ll check it. Thanks for reply.

Edit:
Checked MusicPlayer. It’s present on every scene, Options included. Debug log on default case does not show up.

That’s so weird. I can only suggest zipping up the project and putting it here for others to see where the problem lies (or hopefully someone who has come across this issue knows what the problem is).

Hi @WiesiekRomanowski , have a read of this and see if you can put your project where we can get it. Makes debugging a lot easier. PSA: How to ask for help with Unity

I’m setting up my GitHub account with Unity. I’ve tried to upload the project zip file on Git, but it said it’s too big. So here you have zip file from another source, and when I figure out GitHub (it’s a new tool to me), will post the link to repository.

link to zip file

There are some unfinished features in the game, don’t bother about that. For now the only problem is music not playing in options screen, I will handle the rest. :slight_smile:

Hey @WiesiekRomanowski the problem you’re having with GitHub is that you’re trying to upload the zip file. Git looks at your whole folder structure and tracks the changes on each file individually.

I’m downloading the zip though and I’ll try to have a look at your issue.

Also note that you can significantly reduce the size of your zip by deleting the Library and Temp folders before zipping. These are recreated by Unity as needed.

Here you have a link to GitHub repo. Don’t ask why it has different name, please. :slight_smile:

https://github.com/WiesiekRomanowski/BlockSmasher

1 Like

OK that was a tough one. Your problem is in OptionsScreen.OptionsAreaGroup(), on this line:

musicPlayer.MusicTrackSelection();

Now it’s important to note that OptionsAreaGroup() is being called every frame OnGUI(). So every frame, it’s calling musicPlayer.MusicTrackSelection(), which runs a switch statement and calls bgMusic.Play().

Your problem isn’t that it’s not playing the music, it’s that it’s restarting the music every frame.

Now that you know that, you should be able to figure out how to fix it so it only calls bgMusic.Play() if the clip changes. Good luck!

Incidentally I found it by putting breakpoints throughout MusicPlayer and stepping through the debugger. I’d recommend you look into how that works, it’s a very useful tool.

3 Likes

I’ve putted logs between every line of MusicTrackSelection() method before, and it showed that given track is starting every frame, but I didn’t connect the dots as you did. Didn’t have opportunity to sit on this today, 'll be at home late. Thanks for the tips anyway. :slight_smile:

1 Like

Had last few days off due to sickness, but made it today within less than one hour. :slight_smile:

Here’s final version of MusicTrackSelection() method, finally it’s working.

public void MusicTrackSelection() 
{
	switch(musicTrackInt)
	{
		case 0:
			bgMusic.clip = bgMusic1;
			if(!bgMusic.isPlaying)
			{
				bgMusic.Play();
			}
			break;
		case 1:
			bgMusic.clip = bgMusic2;
			if(!bgMusic.isPlaying)
			{
				bgMusic.Play();
			}
			break;
		case 2:
			bgMusic.clip = bgMusic3;
			if(!bgMusic.isPlaying)
			{
				bgMusic.Play();
			}
			break;
		case 3:
			bgMusic.clip = bgMusic4;
			if(!bgMusic.isPlaying)
			{
				bgMusic.Play();
			}
			break;
		case 4:
			bgMusic.clip = bgMusic5;
			if(!bgMusic.isPlaying)
			{
				bgMusic.Play();
			}
			break;
	}
}

Anyway it wasn’t easy. :slight_smile: I didn’t figure it out until put log:

Debug.Log("track is playing: " + musicPlayer.bgMusic.isPlaying);

into OptionsScreen’ OnGui() method.

Once more thanks for the tips. :slight_smile:

1 Like

Privacy & Terms