0

I have been trying to load a couple of AudioClips using the _Resources_ folder. It seemed to be easy, however, it does not work for me properly, I have followed many tutorials and viewed many questions and their solution, but dead-end still.

I am using a Sound Manager Script to play sounds, in which I create references to Audio Clips and initialize them, and an Audio source. Script:

public static AudioClip flashLightSwitchOnSound, flashLightSwitchOffSound;
static AudioSource audioSrc;
// Use this for initialization
void Start ()
{
    audioSrc = GetComponent<AudioSource> (); 
    flashLightSwitchOnSound = Resources.Load<AudioClip> ("sounds/flashlight_sound/large_flashlight_switched_on"); //Error appears here
    flashLightSwitchOffSound = Resources.Load<AudioClip> ("sounds/flashlight_sound/large_flashlight_switched_off"); //Error appears here
}

// Update is called once per frame
void Update ()
{

}

public static void PlaySound (string clip)
{
    switch (clip) {
    case "FlashlightSwitchOn":
        audioSrc.PlayOneShot (flashLightSwitchOnSound);
        break;
    case "FlashlightSwitchOff":
        audioSrc.PlayOneShot (flashLightSwitchOffSound);
        break;
    }
}

That is how I use the above script in the Flashlight script:

if (Input.GetKeyUp(KeyCode.F))
{
    FLight.enabled = !FLight.enabled;

    if (!isOn)
    {
        isOn = true;
        SoundManagerScript.PlaySound("FlashlightSwitchOn");

    }
    else
    {
        isOn = false;
        SoundManagerScript.PlaySound("FlashlightSwitchOff");
    }

}

When I start the game and start pressing F, it results:

NullReferenceException: Object reference not set to an instance of an object

It appears when loading the audio files in SoundManagerScript. Check the script it's commented.

File Structure:

enter image description here

Andrew Naguib
  • 3,978
  • 2
  • 21
  • 42
  • The error should be the title not how to load audio since that part is fine and error is your issue. If you get error in Unity, double click on it and it will take you to the line of that error. Tell us what that line of error is. – Programmer Feb 24 '18 at 13:38
  • I added a comment where the error appears. – Andrew Naguib Feb 24 '18 at 13:40
  • Sorry. Error cannot appear on the two lines you said you are getting the errors. Make sure to save the script then try again. Until you find out where the error is, it wll be hard to help you – Programmer Feb 24 '18 at 13:43

1 Answers1

1

First, you should probably check where you're getting the 'NullReferenceException'. It may come out that you're correctly loading the resources (As it seems, also if you haven't any typos in your file names).

Are you sure that the GameObject which has the 'SoundScriptManager' component attached also has the 'AudioSource' component attached? Otherwise you would get an exception on the lines:

audioSrc.PlayOneShot(..);

Due to 'audioSrc' not being initialized by the 'GetComponent' method call. If that's the case, you can add this attribute to the 'SoundScriptManager' class in order to let Unity know that the Manager's GameObject requires the 'AudioSource' component attached:

[RequireComponent(typeof(AudioSource))]
ZzAtomiic
  • 181
  • 1
  • 9