Hey, I made script for music list so every time I press one # key it change songs.
the first one is the audiomanager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
#region Static Instance
private static AudioManager instance;
public static AudioManager Instance
{
get
{
if (instance == null)
{instance
= FindObjectOfType<AudioManager>();
if (instance == null)
{
instance = new GameObject("Spawned AudioManager", typeof(AudioManager)).GetComponent<AudioManager>();
}
}
return instance;
}
private set
{
instance = value;
}
}
#endregion
#region Fields
[SerializeField] private List<AudioSource> audioClips;
private AudioSource musicSource;
#endregion
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
musicSource = this.gameObject.AddComponent<AudioSource>();
musicSource.loop = true;
}
private AudioSource GetActiveSource()
{
AudioSource activeSource = (musicSource);
for (int i = 0; i < audioClips.Count; i++)
{
if (audioClips[i].isPlaying)
{
activeSource = audioClips[i];
break;
}
}
if (activeSource == null)
{
activeSource = audioClips[0];
}
return activeSource;
}
public void PlayMusic(AudioClip musicClip)
{
AudioSource activeSource = GetActiveSource();
activeSource.clip = musicClip;
activeSource.volume = 1;
activeSource.Play();
}
public void PlayMusicWhiteFade(AudioClip newClip, float transitionTime = 1.0f)
{
AudioSource activeSource = GetActiveSource();
StartCoroutine(UpdateMusicWithFade(activeSource, newClip, transitionTime));
}
private IEnumerator UpdateMusicWithFade(AudioSource activeSource, AudioClip newClip, float transitionTime)
{
if (!activeSource.isPlaying)
activeSource.Play();
float t = 0.0f;
for (t = 0; t < transitionTime; t += Time.deltaTime)
{
activeSource.volume = (1 - (t / transitionTime));
yield return null;
}
activeSource.Stop();
activeSource.clip = newClip;
activeSource.Play();
for (t = 0; t < transitionTime; t += Time.deltaTime)
{
activeSource.volume = (t / transitionTime);
yield return null;
}
}
}
and the second one is the script I put in the camera with the list of songs and the key definition:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManagerTest : MonoBehaviour
{
[SerializeField] private List<AudioClip> audioClips;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
AudioManager.Instance.PlayMusic(audioClips[1]);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
AudioManager.Instance.PlayMusic(audioClips[2]);
}
}
}
there Is no error on the script but on the playmode it doesn’t respond and don’t play or changing any song. when I press on some # key its show me this error:
NullReferenceException: Object reference not set to an instance of an object
AudioManager.GetActiveSource () (at Assets/AudioManager.cs:49)
AudioManager.PlayMusic (UnityEngine.AudioClip musicClip) (at Assets/AudioManager.cs:67)
AudioManagerTest.Update () (at Assets/AudioManagerTest.cs:15)
when I press on this massage its mark this line:
for (int i = 0; i < audioClips.Count; i++)
what is the problem and how can I fix it?
thanks.