How to start over audio on the click of a button.

I am a very new coder in Unity and I have no idea what I am doing. What I have a problem with is restarting music at the click of a button. Currently, whenever I play the game, then pause the menu and go back to the main menu (which is where the audio listener is off) and then go back to playing the game, the audio does not start over, but rather it keeps playing. So when I exit the main menu and go back to the actual game, the music starts playing as if it were always playing in the background (but you cannot hear it when you are in the main menu).
Basically, how do I restart music when I want to go to a new scene from the main menu at the click of a button.

Below are some pictures that I put in just to be safe. The audio script manages audio. The level select script is where I placed all the functions for going to another level when you click the button.

https://learn.unity.com/project/beginning-audio-in-unity

Welcome! Definitely lots of ways to ramp up your skills. There’s not really enough info for me to offer much for your problem. It sounds like (from the description of your problem) there might be a lifetime issue with your audio sources, perhaps they are staying around when they shouldn’t, or something like that.

If you want to try and track down what’s happening yourself, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene, such as to see if your music source is there, or if it is not playing, etc.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

ALSO… These are some great tutorials to start with, just huge firehoses of amazing info, and before long if you put in the effort you will impress yourself with how much you know.

Imphenzia / imphenzia - super-basic Unity tutorial:

https://www.youtube.com/watch?v=pwZpJzpE2lQ

Brackeys super-basic Unity Tutorial series:

https://www.youtube.com/watch?v=IlKaB1etrik

Sebastian Lague Intro to Game Development with Unity and C#: