Why is Unity telling me that all of Audio Clips within a List are null?
I am trying to implement animalese into my team's game with Unity's default audio. I've written a dialogue player script that takes a letter and tells a specified audio source to PlayOneShot()
with it. I am using a List<AudioClip>
to hold 26 different tiny audio .wav files, which is setup in the inspector window (meaning I drag and drop all of my audio clips there).
This script is accessed from another script that is responsible for typing the letters to the screen. I call PlayLetter(letter)
then convert its ascii value to an index between 0-25. I know that my List is being initialize correctly because it returns 26 when I use count, and I am not getting any out-of-bounds exceptions (the letter will always be a lower case letter so I can safely use an ASCII offset number to subtract with).
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
public class DialoguePlayer : MonoBehaviour
{
public bool alternateLetters = true;
private bool letterToggle = false;
[SerializeField]
private AudioSource audioSource;
[SerializeField]
private List<AudioClip> dialogueClips;
public void PlayLetter(char letter)
{
letter = EvaluateLetter(letter);
if (alternateLetters)
{
letterToggle = !letterToggle;
if (!letterToggle)
return;
}
int asciiOffset = 97;
audioSource.PlayOneShot(dialogueClips[(int) letter - asciiOffset]);
}
private char EvaluateLetter(char letter)
{
return IsLetter(letter) ? char.ToLower(letter) : (char) Random.Range(97, 123);
}
private bool IsLetter(char letter)
{
int asciiValue = (int) letter;
return (asciiValue > 64 && asciiValue < 91) || (asciiValue > 96 && asciiValue < 123);
}
}
Here is a screenshot of the inspector window.
This is the warning from the console.
The only possible idea I have for this is that because the game is using an IEnumerator to access it that it's not reading the correct values. It still knows that the count is 26 but all of the audio clips are null
. I've tried using an AudioClip[]
primitive and also a Dictionary<int, AudioClip>
but neither of those worked either.
Thanks for the help!
Your answer
Follow this Question
Related Questions
Why won't my second Audio Source play? 0 Answers
Playing multiple audio clips 2 Answers
oneshot not working 0 Answers
Precise metronome problem 1 Answer