Instanced Prefabs: Audio clip array out of range

Hi guys,

I have prefab characters, each with an audiosource and a tiny script that should play random audioclips from an array. But I always get a “out of range error”.

It has to do something with the instantiated prefabs. When i put my script on an object within the scene it works as expected.

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

public class Person : MonoBehaviour

    private Animator ani;
    private AudioClip[] yeahs;
    public AudioClip[] yeahs2;
    private AudioSource speaker;

    void Start()

       ani = GetComponent<Animator>();
       ani.SetInteger("level", Random.Range(0, 7));
       speaker = GetComponent<AudioSource>();
       yeahs = Resources.LoadAll<AudioClip>("Assets/_Audio/yeah/");
       Debug.Log("YEAHYEAHYEAH" + yeahs);


    void Update()
        new WaitForSeconds(Random.Range(3, 10));
        ani.SetInteger("level", Random.Range(0, 8));
        speaker.PlayOneShot(yeahs2[Random.Range(0, yeahs2.Length -1)]);

any tipps appreciated!


Hello there,

As far as I can tell from this script, you are never populating “yeahs2”, which means it effectively has a length of 0.

Of course, as soon as you try to call PlayOneShot() in your Update(), there is nothing to play. So it gives you a null exception.

Since your yeahs2 variable is public, I am guessing you are assigning it somehow in the inspector (or on the prefab itself). Could you share your instantiation code here? There may be something wrong in there…

Other than that, please make sure you understand how the Update() function works in Unity. Your “new WaitForSeconds[…]” line does nothing, for example. Also, I am assuming you don’t want to set the Integer of your animator to a random number 60 times per second (the rate at which Update is called), or Play your sound that often.

Hope that helps!




Yes, the yeas2 is size 3 and I setup the audioclips within the inspector.
Why do you think the WaitforSeconds() does not work? I am triggering the animation controller of my instanciated prefabs with it. As far as I can tell it works. I know that it blocks the update loop, but I don’t need a Coroutine in that mini script.

As mentioned above, if I put the script on an empty gameobject within the scene my code works.