The thing is that if I replace ‘clip’ with ‘crack’ (the name of the audio clip) in the solution then Monodevelop says that it does not belongs to the current context. I guess that’s because of this part of the answer:
How can I have access to the clip? Maybe I didn’t had enough attention through the lecture, but it wasn’t obvious for me (not the ‘how’ at least), so I’m stuck at this point. Thanks in advance.
clip is the AudioClip. It is the variable declared at the top of the script, you drag the AudioClip into the exposed field in the Inspector. It is the same as crack, just a different named variable.
Thanks for the reply. I followed the instruction and declared an AudioClip variable, deleted the Audio Source component I had in the brick inspector and dragged the clip into the script’s exposed field. It playbacks the sound but the speaker icon still displays in all bricks at the same time, so PlayClipAtPoint doesn’t seem to be working. Here’s my code:
public class Brick : MonoBehaviour {
private int timesHit;
private LevelManager levelManager;
private bool isBreakable;
public Sprite [] hitSprites;
public AudioClip clip;
public static int breakableCount = 0;
// Use this for initialization
void Start ()
{
isBreakable = (this.tag == "Breakable");
//Keep track of breakable bricks
if (isBreakable) {
breakableCount++;
}
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update ()
{ }
void OnCollisionExit2D (Collision2D collision)
{
if (isBreakable) {
HandleHits();
AudioSource.PlayClipAtPoint (clip, transform.position);
}
}
void HandleHits (){
timesHit = timesHit + 1;
int maxHits = hitSprites.Length + 1;
if (timesHit >= maxHits) {
breakableCount--;
levelManager.BrickDestroyed ();
Destroy (gameObject);
} else {
LoadSprites ();
}
}
void LoadSprites ()
{
int spriteIndex = timesHit - 1;
if (hitSprites [spriteIndex]) {
this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
}
}
// TODO Remove this method once we can actually win
void SimulateWin (){
levelManager.LoadNextLevel();
}
}