-2

I am getting this weird error in unity when I am trying to run my (sad excuse for a) game. Here's the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ARsound : MonoBehaviour
{

    public static AudioClip sound;
    static AudioSource audioSrc;


    void Start()
    {
        sound = Resources.Load<AudioClip>("AR SFX");
        audioSrc = GetComponent<AudioSource>();
    }

    public static void playSound()
    {
        audioSrc.PlayOneShot(sound);
    }


}

And the error is this:

PlayOneShot was played with a null audio clip

Even though that audio clip doesn't SEEM to be null. Here is some additional info:

Gun:Update() (at Assets/Scripts/Gun.cs:17)

Suggestions?

GraniteSOS
  • 49
  • 2
  • 10
  • 2
    There is no `while` loop here. It's also not at all clear how your question title matches your question body. – ProgrammingLlama Jun 04 '20 at 07:04
  • Where is the while loop !!? – Ahmed Soliman Jun 04 '20 at 07:06
  • I didn't mean while loop. Oh no! That was from a different question I was going to ask. It's 3 A.M. So I'm kinda tired. I'm SO sorry. I changed the title to what I wanted it to be. – GraniteSOS Jun 04 '20 at 07:11
  • Have you considered that `Resources.Load("AR SFX");` failed so `sound` is simpy `null`? The additional info `Gun:Update() (at Assets/Scripts/Gun.cs:17)` doesn't help at all since I think this is just the place where you are calling `ARSound.playSound();` ... – derHugo Jun 04 '20 at 08:17
  • I figured it out! I did it a little differently. First I declared two audio sources ```public AudioSource shootSound; public AudioSource shootFastSound; ```Then, I did ```shootSound = GetComponent(); shootFastSound = GetComponent();``` And lastly, I did ```private void ShootFast() { shootFastSound.Play(); // Other Code } – GraniteSOS Jun 04 '20 at 20:15

1 Answers1

1

Your call to Resources.Load returns null - this happens when the asset is not found (no exception).

See docs: https://docs.unity3d.com/ScriptReference/Resources.Load.html

So you need to make sure that the asset exists and that it can be loaded.

Lucero
  • 59,176
  • 9
  • 122
  • 152