Hello @Rob i need help to fix this error .
I fixed it myself
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
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
Regarding the ball not going “boing”… can you post the full Ball.cs script as it is as this time please.
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
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.
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;
- Forum User Guides : How to apply code formatting within your posts
It worked! thank you so much
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
Yes, because of typo, i get in trouble a lot of time but I’m glad that I’m moving forward with course and getting understand
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