я кнопкам повесил 2 события, 1 событие - функция проигрывания звука, 2 - другое
и это на 1 сцене, потом я на 2 перешел и снова на 1 и у мя звук перестал проигрываться при клике, но 2 функция которая другое делает нормально работала, что не так?
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AudioManager : MonoBehaviour
{
public static AudioManager Instance;
public Sound[] sfxSounds, musicSounds;
public AudioSource sfxSource, musicSource;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
}
else
{
Destroy(gameObject);
return;
}
if (!PlayerPrefs.HasKey(PlayerPrefsKeys.MusicVolume))
PlayerPrefs.SetFloat(PlayerPrefsKeys.MusicVolume, 0.28f);
if (!PlayerPrefs.HasKey(PlayerPrefsKeys.SfxVolume))
PlayerPrefs.SetFloat(PlayerPrefsKeys.SfxVolume, 1f);
musicSource.volume = PlayerPrefs.GetFloat(PlayerPrefsKeys.MusicVolume);
sfxSource.volume = PlayerPrefs.GetFloat(PlayerPrefsKeys.SfxVolume);
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name == "Menu")
{
PlayMusic("MenuMusic");
}
else if (scene.name == "PvpArena")
{
PlayMusic("PvpMusic");
}
}
public void PlayMusic(string name)
{
Sound s = Array.Find(musicSounds, x => x.name == name);
if (s == null)
{
Debug.Log("Sound not found");
}
else
{
musicSource.clip = s.clip;
musicSource.Play();
}
}
public void PlaySfx(string name)
{
Sound s = Array.Find(sfxSounds, x => x.name == name);
if (s == null)
{
Debug.Log("Sound not found");
}
else
{
sfxSource.PlayOneShot(s.clip);
}
}
}