basically, its as the title says. I am trying to build a brick breaker game in Unity2D, and I have found myself unable to play the death sound effect (played whenever a brick breaks).
using UnityEngine;
public class AudioManager : MonoBehaviour
{
// Start is called before the first frame update
public AudioClip[] sounds;
public AudioSource[] sauce;
public GameObject brick;
void Awake()
{
foreach(AudioClip sound in sounds) {
AudioSource source = this.gameObject.AddComponent<AudioSource>();
source.clip = sound;
}
sauce = GetComponents<AudioSource>();
}
void Start() {
}
// Update is called once per frame
void Update()
{
Debug.Log(sauce[0]);
}
public void BrickDeath() {
sauce[0].Play();
}
}
This is my audiomanager script, the Debug.Log statement in the Update() function clearly shows that there is an AudioSource object in the array.
However, the strange thing is when I reference the AudioManager game object in my Brick game object as so
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Brick : MonoBehaviour
{
// Start is called before the first frame updates
public int health;
public Sprite brokenBrick;
SpriteRenderer sr;
public GameObject audioManager;
AudioManager manager;
private void Start()
{
sr = GetComponent<SpriteRenderer>();
manager = audioManager.GetComponent<AudioManager>();
}
// Update is called once per frame
private void Update()
{
if (health < 15) {
sr.sprite = brokenBrick;
} if (health <= 0) {
manager.BrickDeath();
Death();
}
}
private void Death() {
Destroy(this.gameObject,1f);
}
void OnTriggerEnter2D(Collider2D other)
{
health -= 10;
}
}
it gives me this error
IndexOutOfRangeException: Index was outside the bounds of the array.
AudioManager.BrickDeath () (at Assets/Scripts/AudioManager.cs:43)
Brick.Update () (at Assets/Scripts/Brick.cs:26)
This is the setup of my inspector for the AudioManager

and this is for my brick object.
Solutions would be greatly appreciated. Thank you in advance and sorry for the rather poor formatting.
