It looks like you're new here. If you want to get involved, click one of these buttons!
Hi,
I need to stop playing background sound "Theme", and switch to new sound on each scene load. How am I able to do that?
I am using the audio manager from Brackeys used in this video: https://www.youtube.com/watch?v=6OT43pvUyfY.
I've found this code and I added it to the manager, unfortunatelly it does not work:
public void StopPlaying (string sound)
{
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
//s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
//s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
s.source.Stop ();
}
I've tried switching the sound by this script:
void Awake()
{
int level = SceneManager.GetActiveScene().buildIndex;
switch(level)
{
case 0:
FindObjectOfType<AudioManager>().Play("Menu");
break;
case 1:
FindObjectOfType<AudioManager>().StopPlaying("Menu");
FindObjectOfType<AudioManager>().Play("Level_Select");
break;
case 2:
FindObjectOfType<AudioManager>().StopPlaying("Level_Select");
FindObjectOfType<AudioManager>().Play("Theme");
break;
case 3:
FindObjectOfType<AudioManager>().StopPlaying("Level_Select");
FindObjectOfType<AudioManager>().Play("Theme");
break;
case 4:
FindObjectOfType<AudioManager>().StopPlaying("Level_Select");
FindObjectOfType<AudioManager>().Play("Theme");
break;
case 5:
FindObjectOfType<AudioManager>().StopPlaying("Level_Select");
FindObjectOfType<AudioManager>().Play("Theme");
break;
case 6:
FindObjectOfType<AudioManager>().StopPlaying("Level_Select");
FindObjectOfType<AudioManager>().Play("Theme");
break;
case 7:
FindObjectOfType<AudioManager>().StopPlaying("Level_Select");
FindObjectOfType<AudioManager>().Play("Theme");
break;
case 8:
FindObjectOfType<AudioManager>().StopPlaying("Level_Select");
FindObjectOfType<AudioManager>().Play("Theme");
break;
case 9:
FindObjectOfType<AudioManager>().StopPlaying("Level_Select");
FindObjectOfType<AudioManager>().Play("Theme");
break;
case 10:
FindObjectOfType<AudioManager>().StopPlaying("Theme");
FindObjectOfType<AudioManager>().Play("Win");
break;
}}
How do I switch the sound when new level loads, insted of the same level reloading?
I would also love to increase the pitch, with each game level (level 8 being pitch 1).
Build indexes:
0 = menu
1 = level_select
2 = level 1
3 = level 2
.......
9 = level 8
10 = credits
Thanks for all answers,
TG