Straight to the point. The following code is in a script that executes before all other scripts:
public static AudioClip[] FootstepSounds;
void Start()
{
FootstepSounds = Resources.LoadAll("Sound Library/Footstep Sounds", typeof(AudioClip)) as AudioClip[];
}
The above always worked for me and the path is correct. I then use this Array in another script like this:
AudioClip currentAudioClip;
void Footstep() // I'm using this as a Mecanim Event on a walk animation
{
Debug.Log("List null? -> " + SourceScript.FootstepSounds == null); // returns "List null? -> false"
Debug.Log("List length -> " + SourceScript.FootstepSounds.Length); // NullReferenceException
// of course NullReferenceException at the following line too
currentAudioClip = SourceScript.FootstepSounds[Random.Range(0, SourceScript.FootstepSounds.Length)];
}
The compiler tells me that the collection is not null, so it must be the elements, which were obviously provided inside the source script. Does anyone know what's going on?
↧