I have an AudioManager for my Game. When I try to play a clip on a NPC it doesn't get played. The script itself works because the background music is playing properly.
The code of the relevant parts:
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Random = UnityEngine.Random;
public class AudioManager : MonoBehaviour
{
private AudioSource audioSource;
private AudioSource backgroundPlayer;
private Dictionary<string, Sound> audioClips;
public List<Sound> background;
void Start()
{
Sound current = background[background.Count-1];
backgroundPlayer.clip = current.clip;
backgroundPlayer.volume = current.volume;
backgroundPlayer.pitch = current.pitch;
backgroundPlayer.Play(); //works absolutely fine
}
public void SetCurrentSource(AudioSource a)
{
audioSource = a;
}
public void Play(string name, string NPCSpeaker)
{
string actualName = name + "_" + NPCSpeaker;
if (audioClips.ContainsKey(actualName))
{
audioSource.clip = audioClips[actualName].clip;
audioSource.volume = audioClips[actualName].volume;
audioSource.pitch = audioClips[actualName].pitch;
Debug.Log("Start Audio " + audioSource.clip);
audioSource.Play();
Debug.Log(audioSource.isPlaying);
}
else
{
Debug.LogWarning("AudioClip with name "+ actualName + " is not found." );
}
}
And the Sound Class:
public class Sound
{
public string name;
public AudioClip clip;
[Range(0f, 1f)]
public float volume;
[Range(.1f, 3f)]
public float pitch;
}
Before I call Play()
, I always call setCurrentSource()
because the audioSource
is the current NPC in the conversation.
Clip, volume and pitch are set correctly and are not null.
I found out, when I debug it, set a breakpoint in Play()
somewhere and go to the point of calling audioSource.Play()
, then open the inspector to view the object informations in VS Code, it will play the audio. If i just wait at audioSource.Play()
it doesn't.
The Debug statement Debug.Log(audioSource.isPlaying)
returns false.
As said I can hear the music, so Editor isn't muted. Gameobject with the AudioSource Component is activated.
Edit: Creating the AudioClips:
void Awake()
{
backgroundPlayer = gameObject.GetComponent<AudioSource>();
createAudioClips();
audioClips = new Dictionary<string, Sound>();
foreach (Sound s in sounds)
{
audioClips.Add(s.name, s);
}
}
private void createAudioClips()
{
string rel = Application.streamingAssetsPath + "/Audio/";
string[] paths = new string[] { rel + "Brian", rel + "Matthew", rel + "Russell", rel + "Leader", rel + "Scientist" };
foreach (string p in paths)
{
foreach (string fileName in Directory.GetFiles(p))
{
if (Path.GetExtension(fileName) == ".mp3")
{
if (p == rel + "Scientist")
{
CreateNewSound(fileName, true);
}
else
{
CreateNewSound(fileName, false);
}
}
}
}
}
private void CreateNewSound(string filename, bool isScientist)
{
WWW request = new WWW(filename);
string[] subs = filename.Split('\\');
Sound newSound = new Sound();
newSound.name = Path.GetFileNameWithoutExtension(filename);
newSound.clip = request.GetAudioClip();
if (isScientist)
{
newSound.volume = 1f;
}
else
{
newSound.volume = 0.5f;
}
newSound.pitch = 1f;
sounds.Add(newSound);
}
The Soundfiles are .mp3 and are in the StreamingAssets Folder.