Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Music problem

Discussion in 'Scripting' started by unity_F3F23D39B27A5B4AB766, May 27, 2024.

  1. unity_F3F23D39B27A5B4AB766

    unity_F3F23D39B27A5B4AB766

    Joined:
    Mar 20, 2024
    Posts:
    5
    Hey Everyone, recently i've been trying to make a Menu Music that doesn't destroy on load while switching through scenes, and i accomplished that, however, now the problem is that i want the Menu music to stop when you get to the actual game scene. The gameobject that doesn't destroy on load has 2 audiosources because the menu music has an "Intro" and a "loopable" music.

    Here is the Script, The Update method that is on the Game scene, and the error i've been getting:

    Code (CSharp):
    1. public class MenuAudioManager : MonoBehaviour
    2. {
    3.     public static MenuAudioManager instance;
    4.  
    5.     public AudioSource bgmsource;
    6.     public AudioSource bgmloopsource;
    7.  
    8.     public AudioClip clipStart;
    9.     public AudioClip clipLoop;
    10.  
    11.     private Coroutine bgmCoroutine;
    12.  
    13.     private void Awake()
    14.     {
    15.         SetUpMusic();
    16.     }
    17.  
    18.     void Start()
    19.     {
    20.         bgmCoroutine = StartCoroutine(BGMLoopAlt(0f));
    21.     }
    22.  
    23.  
    24.     void SetUpMusic()
    25.     {
    26.         if(FindObjectsOfType(GetType()).Length > 1)
    27.         {
    28.             Destroy(gameObject);
    29.         }
    30.         else
    31.         {
    32.             DontDestroyOnLoad(gameObject);
    33.         }    
    34.     }
    35.  
    36.  
    37.     IEnumerator BGMLoop(float delay)
    38.     {
    39.         clipLoop.LoadAudioData();
    40.         yield return new WaitForSeconds(delay);
    41.         bgmsource.clip = clipStart;
    42.         bgmsource.Play();
    43.         yield return new WaitForSeconds(clipStart.length);
    44.         bgmsource.clip = clipLoop;
    45.         bgmsource.loop = true;
    46.         bgmsource.Play();
    47.         Debug.Log(clipStart.length + " lunghezza clipstart");
    48.  
    49.  
    50.         yield return null;
    51.     }
    52.  
    53.  
    54.     IEnumerator BGMLoopAlt(float delay)
    55.     {
    56.         yield return new WaitForSeconds(delay);
    57.         bgmsource.clip = clipStart;
    58.         bgmloopsource.clip = clipLoop;
    59.         bgmloopsource.loop = true;
    60.         bgmsource.Play();
    61.         //yield return new WaitForSeconds(clipStart.length);
    62.  
    63.         bgmloopsource.PlayDelayed(clipStart.length);
    64.         Debug.Log(clipStart.length + " lunghezza clipstart");
    65.  
    66.  
    67.         yield return null;
    68.     }
    69. }
    Code (CSharp):
    1.    void Update()
    2.     {
    3.         if (SceneManager.GetActiveScene().name == "Town")
    4.             MenuAudioManager.instance.GetComponent<AudioSource>().Pause();
    5.     }

    NullReferenceException: Object reference not set to an instance of an object
    GameController.Update () (at Assets/_MyAssets/Scripts/Controller e Utilities/GameController.cs:93)


    (line 93 is the update method)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,369
    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that




    If you just wanna use a nice simple music manager with fading and no coroutines, here's mine:

    https://gist.github.com/kurtdekker/3e81492863526c160077a16957369a94

    Includes a sample test script that lets you switch between two songs and silence.
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,687