0
\$\begingroup\$

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.

enter image description here 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 enter image description here

and this is for my brick object.

enter image description here

Solutions would be greatly appreciated. Thank you in advance and sorry for the rather poor formatting.

\$\endgroup\$
2
  • \$\begingroup\$ Why is there an brick on your audiomanager? \$\endgroup\$ Commented Apr 28, 2022 at 5:11
  • \$\begingroup\$ @Zibelas earlier i tried referencing it so that I could get the script component of the Brick and access it's health property, so I can play the sound upon death. it appears I have forgotten to remove it. \$\endgroup\$ Commented Apr 28, 2022 at 5:27

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.