I’ve been fighting with this script for a while and decided that I need help I have a variable that is being tested that represent the level that the player is on, in the beginning (the loading page a null level) The Title page (variable set as 0) The level select and throughout the levels the variable changes (variable = 1 for level 1, variable = 2 for level 2, etc…) This script has a no delete portion to keep it from deleting between rooms to keep a constant flow of music. What I am having issues with is getting the music to play.
float lev = 1f;
public AudioClip intro;
public AudioClip menu;
public AudioClip L1to9;
public AudioClip L10to23;
void Start () {
}
void Update () {
if (lev == null) {
intro.Play ();
}
if (lev == 0) {
menu.Play ();
}
You should attach the clips to an AudioSource. If you only need one of them playing at a time, you can use a single AudioSource object. Something like this:
//attached an AudioSource component in the inspector..
private AudioSource audioSource;
float lev = 0f;
public AudioClip intro;
public AudioClip menu;
public AudioClip L1to9;
public AudioClip L10to23;
int currentLev = 0;
void Start()
{
//assign AudioSource component to audioSource variable
audioSource = GetComponent<AudioSource>();
audioSource.clip = intro;
audioSource.Play();
}
If at some point you wish to change the clip, just do:
audioSource.Stop()
audioSource.clip = menu;
audioSource.Play();
But be careful with what you do in the Update() function. The code you posted, assuming that you meant to call AudioSource.Play(), will call trigger Play() every time the Update() function is called. You only need to call Play() once when you want the audio to start, not on every frame. Btw, lev can be an int, and use 0 for the loading page. null won’t work.
1 Like
Thank you so much it worked exactly how I wanted to.