Need help with audio.
So I'm kinda a noob to c# and unity and am having trouble with audio. I want to have background music for the menu and different music for the gameplay levels. I'm using a bunch of if statements to check the scene then play the correct music but it's not working and I have no idea why. Please Help! My Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class AudioManager : MonoBehaviour {
public AudioSource audioSource;
public AudioClip starryNight;
public AudioClip stealthMode;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (audioSource.isPlaying == false)
{
if (SceneManager.GetActiveScene().name == "Main Menu")
{
audioSource.clip = starryNight;
audioSource.Play();
}
if (SceneManager.GetActiveScene().name == "Level Selection")
{
audioSource.clip = starryNight;
audioSource.Play();
}
if (SceneManager.GetActiveScene().name == "Level 1")
{
audioSource.clip = stealthMode;
audioSource.Play();
}
if (SceneManager.GetActiveScene().name == "Level 2")
{
audioSource.clip = stealthMode;
audioSource.Play();
}
if (SceneManager.GetActiveScene().name == "Level 3")
{
audioSource.clip = stealthMode;
audioSource.Play();
}
if (SceneManager.GetActiveScene().name == "Level 4")
{
audioSource.clip = stealthMode;
audioSource.Play();
}
if (SceneManager.GetActiveScene().name == "Level 5")
{
audioSource.clip = stealthMode;
audioSource.Play();
}
if (SceneManager.GetActiveScene().name == "Level 6")
{
audioSource.clip = stealthMode;
audioSource.Play();
}
}
}
This script makes no sense.
Why don't you have a simple empty with an AudioSource component attached, with the appropriate AudioClip already set + PlayOnAwake enabled?
Your answer
Follow this Question
Related Questions
Can You help me Fix This? (Infinite level Generator) 0 Answers
Trying to get the doors to play the opening and closing sounds seperately/individually 1 Answer
Can not change isTrigger to true 1 Answer
Button problem : changing scenes fail ? 0 Answers
How to move whilst airborne (using standard third person character script)? 0 Answers