Howdy ![]()
I have a problem with sounds in my project. After applying the scripts provided below (from a tutorial on YT), the sound files are not visible in Unity. I checked and they work fine. In the previous 2D project the same scripts worked great and the entire sound system was correct, so I have no idea where the problem could be?
I will add that the background music works properly. I tried applying sounds in a different way, for example: when an object with the tag "Player" touches the object, the sound X will play.
There is no bug in this diagram either, but the sounds are not visible and I cannot add them.
Scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using System;
public class AudioManager : MonoBehaviour {
public static AudioManager instance;
public Sound[] sounds;
void Awake ()
{
if (instance != null)
{
Destroy(gameObject);
return;
} else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
s.source.outputAudioMixerGroup =s.mixer; // z komentarza na YT
}
}
public void Play(string sound)
{
Sound s = Array.Find(sounds, item => item.name == sound);
s.source.Play();
}
public void Stop(string sound)
{
Sound s = Array.Find(sounds, item => item.name == sound);
s.source.Stop();
}
}
and next:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
[System.Serializable]
public class Sound
{
public string name;
public AudioClip clip;
public AudioMixerGroup mixer;
[Range(0f, 1f)]
public float volume = 1;
[Range(-3f, 3f)]
public float pitch = 1;
public bool loop = false;
[HideInInspector]
public AudioSource source;
}