Section 5, lecture 89

Hello @Rob i need help to fix this error .

I fixed it myself :grin:

The error came because of not adding these methods;

public AudioClip boing;	

// under the class.

 AudioSource.PlayClipAtPoint (boing, transform.position);

// under OnCollisionEnter2D.

I looked at brick sound method and just add in into ball script, so the error is gone but the ball is not boing :confused:

Well done on fixing the error yourself, that is always a great feeling, and sometimes you just need to come back to the issue after a little break with fresh eyes :slight_smile:

Regarding the ball not going “boing”… can you post the full Ball.cs script as it is as this time please.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {

	public AudioClip boing;
	private Paddle paddle;
	private bool hasStarted = false;
	private Vector3 paddleToBallVector;

	// Use this for initialization
	void Start () {
		paddle = GameObject.FindObjectOfType<Paddle>();
		paddleToBallVector = this.transform.position - paddle.transform.position; 

	// Update is called once per frame
	void Update () {
		if (!hasStarted) {
			// lock the ball to the paddle.
			this.transform.position = paddle.transform.position + paddleToBallVector;

			// wait for the mouse press to lunch.
		    if (Input.GetMouseButtonDown(0)) {
			    print("mouse clicked, launced ball");
			    hasStarted = true;
				this.GetComponent<Rigidbody2D>().velocity = new Vector2 (2f, 10);

	void OnCollisionEnder2D (Collision2D collision) { 
		// ball does not trigger when brick is destroyed.
		// Not 100% sure why, possibly because brick is not there.
		Vector2 tweak = new Vector2 (Random.Range(0f, 0.2f), Random.Range(0f,0.2f));
		print (tweak);
		if (hasStarted) {
			AudioSource.PlayClipAtPoint (boing, transform.position);
			this.GetComponent<Rigidbody2D>().velocity += tweak;

Here is the Ball script :slight_smile:

Hi @Sabeen_Moin,

Remember the other day when I spotted the typo and pointed it out to you - you have the same one in this script also;

void OnCollisionEnder2D (Collision2D collision) {  

That should read;

void OnCollisionEnter2D (Collision2D collision) { 

The code within that method would not have been being called at all, correct the method name and see if that resolves the issue.

If it does, check your other scripts also for the same issue, you may have copy/pasted the same methods from one script to another, hence the same error/issue etc.

Hope this helps. :slight_smile:

Also, when pasting your code into your posts, if you enter three ` characters before it, and three after it, all of the code between will be formatted correctly (I have edited several of your posts to make it easier to read for the person helping you).

See also;

1 Like

It worked!:grin: thank you so much :slight_smile:

1 Like

You’re more than welcome @Sabeen_Moin.

Remember, check your other scripts as well and see if the same issue occurs anywhere else, this may save you some time moving forwards :slight_smile:

1 Like

Yes, because of typo, i get in trouble a lot of time :sweat_smile: but I’m glad that I’m moving forward with course and getting understand :slight_smile::innocent:

1 Like

Always worth checking things like that as part of your first round of debugging, especially if you know it has tripped you up before.

Glad to hear things are going well :slight_smile:

1 Like

Privacy & Terms