Nyxie
Nyxie

Reputation: 15

Unity is saying Audioclip is null

I am using this script to try and pull random noises to play during gameplay

    public class Sons : MonoBehaviour
    {
    public static AudioClip al1, al2, al3, al4, al5, al6, al7, al8, al9, al10, al11, al12, al13;
    static AudioSource Audio;
    int randomizer;
    // Start is called before the first frame update
     void Start()
     {
        randomizer = (Random.Range(1, 13));
        //sons aleatorios=============================================================
        al1 = Resources.Load<AudioClip>("ran1");
        al2 = Resources.Load<AudioClip>("ran2");
        al3 = Resources.Load<AudioClip>("ran3");
        al4 = Resources.Load<AudioClip>("ran4");
        al5 = Resources.Load<AudioClip>("ran5");
        al6 = Resources.Load<AudioClip>("ran6");
        al7 = Resources.Load<AudioClip>("ran7");
        al8 = Resources.Load<AudioClip>("ran8");
        al9 = Resources.Load<AudioClip>("ran9");
        al10 = Resources.Load<AudioClip>("ran10");
        al11 = Resources.Load<AudioClip>("ran11");
        al12 = Resources.Load<AudioClip>("ran12");
        al13 = Resources.Load<AudioClip>("ran13");

        InvokeRepeating("RNDSND", 1f, 20f);
        //sons ventilação============================================================

        //sons corredor==============================================================

        //sons salas=================================================================

        Audio = GetComponent<AudioSource>();

      }

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

    }
    void RNDSND()
    {
        switch (randomizer)
        {
            case 1:
                Audio.PlayOneShot(al1);
                 break;
            case 2:
                Audio.PlayOneShot(al2);
                break;
            case 3:
                Audio.PlayOneShot(al3);
                break;
            case 4:
                Audio.PlayOneShot(al4);
                break;
            case 5:
                Audio.PlayOneShot(al5);
                break;
            case 6:
                Audio.PlayOneShot(al6);
                break;
            case 7:
                Audio.PlayOneShot(al7);
                break;
            case 8:
                Audio.PlayOneShot(al8);
                break;
            case 9:
                Audio.PlayOneShot(al9);
                break;
            case 10:
                Audio.PlayOneShot(al10);
                break;
            case 11:
                Audio.PlayOneShot(al11);
                break;
            case 12:
                Audio.PlayOneShot(al12);
                break;
            case 13:
                Audio.PlayOneShot(al13);
                break;

        }
    }
}

...but unity sends the message "PlayOneShot Was Called With Null AudioClip"

Can anyone point out what I am doing wrong and any possible solutions?

Upvotes: 1

Views: 1226

Answers (1)

gen
gen

Reputation: 147

I would recommend doing something more like this:

public class Sons : MonoBehaviour
{
    public AudioClip[] randSounds;
    static AudioSource Audio;
    int randomizer;
    // Start is called before the first frame update
    void Start()
    {
        for(int i = 0; i < randSounds.Length; i++)
        {
            randSounds[i] = Resources.Load<AudioClip>("ran" + i);
        }

        Audio = GetComponent<AudioSource>();

        InvokeRepeating("RNDSND", 1f, 20f);
    }

    void RNDSND()
    {
        randomizer = (Random.Range(0, randSounds.Length));
        Audio.PlayOneShot(randSounds[randomizer]);        
    }
}

Untested. I would probably also just delete the Resources.Load call since it seems expensive memory wise to do that on load for all your objects, you probably want to assign those clips into the inspector if possible, or load them on Awake() and before you need to use them.

Upvotes: 2

Related Questions