PlayClipAtPoint in Unity 5

Hi, so there was this closed post. I encountered the same problem and it looked like a great and simple solution:

Continuing the discussion from PlayClipAtPoint in Unity 5:

The answer:

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.

Note: I’m actually on Unity 2017.

Hi Gabriel,

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.

Perhaps if you post your script?


See also;

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();
	}
}

Screenshot of my Unity main window while running

Thanks again.

Hi,

You haven’t removed the AudioSource component, you have just disabled it by removing the tick.

You don’t need an AudioSource on the Brick GameObjects so you can just remove it completely. This will also remove the speaker icon inyour Scene view.

It worked perfectly. Thank you so much Rob!

1 Like

You are very welcome. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms