When I click on the run button in the unity editor it gives out this error, but when I select gameobject SoundManager all works fine
I recorded a video to make it clear what I mean
NullReferenceException: Object reference not set to an instance of an object
SoundControll.Initial () (at Assets/Scripts/Sounds/SoundControll.cs:48)
SoundControll.Start () (at Assets/Scripts/Sounds/SoundControll.cs:18)
using System;
using UnityEngine;
public class SoundControll : MonoBehaviour
{
public Sound soundAudioClips;
[SerializeField] private AudioSources audioSource;
private void Awake()
{
audioSource = new AudioSources[soundAudioClips.Length];
}
private void Start()
{
Initial();
}
private void OnEnable()
{
EventManager.OnSoundBySoundsControllEvent += OnSoundControllEventHandler;
}
public void PlaySound(Sounds sound)
{
if (!GameManager.instance.Sound)
{
Stop();
return;
}
var clip = Array.Find(audioSource, s => s.sound == sound);
if (clip is null)
{
Debug.LogError($"Sound {sound} not found!");
return;
}
clip?.source?.Play();
}
private void Initial()
{
for (int i = 0; i < audioSource.Length; i++)
{
audioSource*.source = gameObject.AddComponent<AudioSource>();*
audioSource_.sound = soundAudioClips*.sound;
audioSource.source.clip = soundAudioClips.audioClip;
audioSource.source.volume = soundAudioClips.volume;
audioSource.source.loop = soundAudioClips.loop;
}
}
private void OnSoundControllEventHandler(Sounds sound)
{
PlaySound(sound);
}
private void Stop()
{
Array.ForEach(audioSource, s => s.source?.Stop());
}
private void OnDisable()
{
EventManager.OnSoundBySoundsControllEvent -= OnSoundControllEventHandler;
}
[System.Serializable]
public class AudioSources
{
public Sounds sound;
public AudioSource source;
}
}*_