I’m building a simple breakout game and I want to play a sound everytime you hit a block. I’ve tried everything but it keeps saying that it can’t play disabled sounds. Here is a piece of my code from the bricks (note: I’m just a beginner so I may have overlooked things)
using UnityEngine;
using System.Collections;
public class Bricks : MonoBehaviour {
public GameObject brickParticle;
void Awake ()
{
}
void OnCollisionEnter (Collision other)
{
Instantiate(brickParticle, transform.position, Quaternion.identity);
GM.instance.DestroyBrick();
Destroy(gameObject);
}
}
And here is the code for the sound I want to play
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class BrickDestroy : MonoBehaviour {
public AudioClip brickBreak;
private AudioSource source;
// Use this for initialization
void Awake ()
{
source = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision coll)
{
source.PlayOneShot(brickBreak, 1F);
Debug.Log ("Sound Played");
}
}
I’ve searched most forums without success, I really hope someone can help me here. If you need more information, feel free to ask.
Thanks in advance.
EDIT: Does anyone know how to set a delay between the destruction of the bricks and the audio? I figured out when I play the game in the editor and change a timescale in the GM script, the bricks won’t break down but sound does play.