Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why does my sound manager work and then after restarting unity it doesnt?

Discussion in 'Getting Started' started by warrenbrandt, Aug 1, 2019.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    I have a sound manager which i learnt from a tutorial

    it was working, then i saved everything and restarted, and it wasnt working, had the below error.

    if i recreate the script and apply it, it works, but as soon as i restart unity and my project, im getting an error:
    NullReferenceException: Object reference not set to an instance of an object

    and it takes me to this line:
    Code (CSharp):
    1. audioSrc.PlayOneShot(raceStart);
    here is my entire sound manager script

    any ideas why it works and then stops?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class soundManager : MonoBehaviour
    6. {
    7.     public static AudioClip raceStart;
    8.  
    9.     static AudioSource audioSrc;
    10.  
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.         raceStart = Resources.Load<AudioClip>("racestart");
    18.         audioSrc = GetComponent<AudioSource>();
    19.  
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.     }
    27.  
    28.     public static void PlaySound(string clip)
    29.     {
    30.  
    31.         switch (clip)
    32.         {
    33.             case "racestart":
    34.                 audioSrc.PlayOneShot(raceStart);
    35.                 break;
    36.         }
    37.     }
    38.  
    39. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well it's very weird to have this thing be a MonoBehaviour, but then use static properties for your raceStart and audioSrc references. Remove "static" from both of those and I bet it'll work fine. (Static property values stick around for the lifetime of the app, which is Unity itself when running within the editor, so that's why it works until you quit.)

    Of course you've got PlaySound set as static too, so you'll need to make a few other adjustments. Probably you should be using the Singleton pattern instead.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're probably calling PlaySound before Start has run. Maybe the object with this script attached isn't even in the scene. Add debugging, and also check results of all your GetComponent and Resources.Load calls for null before using them.
     
  4. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    ok thanks got it sorted, yes it was played from the wrong objects start function, moved it to where it should be, working fine now so it seems.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You might want to move everything in your Start method here to Awake, so you're sure it is run before any other object's Start method if you're going to play sounds with this from Start.
     
    JoeStrout likes this.