Getting error message

I’m getting this error message when I moved for a new level.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{
[SerializeField] Paddle paddle1;
[SerializeField] float xPush = 2f;
[SerializeField] float yPush = 15f;
[SerializeField] AudioClip ballSound;

//State
Vector2 paddleToBallVector;
bool hasStarted = false;
//Cached component reference
AudioSource myAudioSource;
// Start is called before the first frame update
void Start()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
    myAudioSource = GetComponent<AudioSource>();
}

// Update is called once per frame
void Update()
{
    if (!hasStarted)
    {
        LockBallToPaddle();
        LaunchOnMouseClick();
    }
   
}

private void LaunchOnMouseClick()
{

    if (Input.GetMouseButtonDown(0))
    {
        hasStarted = true;
        GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);

    }
}

private void LockBallToPaddle()
{
    Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    transform.position = paddlePos + paddleToBallVector;
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (hasStarted)
    {
        AudioClip clip = ballSound[UnityEngine.Random.Range(0, ballSound.Length)];
        myAudioSource.PlayOneShot(clip);
    }
}

}

Hi Sibam,

Check your ballSound array. Are there clips assigned to it in its Inspector?

Nope. Is it necessary ?

Of course, it is. How is Unity supposed to know what sounds it is supposed to play if you don’t tell it what to play? :wink:

You may use a conditional to check if there is effectively a GameObject attached, and stop executing code if there’s no object without crashing the execution. Rick recommends that later in the course in reference to VFX particle effects, but it seems to be completely compatible here. You can add that at the very top of the place Unity have to use this attached object inside the inspector. It seems to me a good practice to keep that in mind.

I think a code like that may be ok. If not maybe @Nina please correct me:

private void OnCollisionEnter2D(Collision2D collision)
{
    if (!myAudioSource) { return; } // you can read as: if (there is not myAudioSource) { go on, go on; }
    if (hasStarted)
    {
        AudioClip clip = ballSound[UnityEngine.Random.Range(0, ballSound.Length)];
        myAudioSource.PlayOneShot(clip);
    }
}

Hope it helps!

1 Like

Your code is fine but since you are already checking things, which is great, you could also check the ballSound array and clip. Then your code would be perfect. :wink:

1 Like

But I have assigned sounds with other objects. Whenever ball will collide with them, sound will play. Still should I have to assign a sound to ball also ?

Thank you. I’m on it.

You assigned the Ball script to the Ball game object, didn’t you?

If so, that Ball instance tries to play a sound from its own ballSound array.

Of course I did. But now I cann’t play the game because of errors.

Could you select the ball in the Hierarchy and share a new screenshot of the entire Unity window? I’d like to see the Inspector of the ball and the error messages in your console.

There is an error message in your console. Fix that first. I think it’s still the same issue as this one in your other thread. It is highly recommended to fix errors first before proceeding with your game.

I cannot see the Ball component in your screenshot. Could you collapse the Rigidbody2D and the collider?

But every time it’s showing me some sound code. But in visual studio there’s no errors showing

Does MonoBehaviour have the same colour as your classname? If not, there might be a problem with Visual Studio. Please watch lecture “Fixing Visual Studio Problems” (currently #4).

Where did you type RigidBody2D? Did you mean Rigidbody2D?

Okay. Let me repeat the lectures.