It means the code you gave. And error I’m getting is this
After doing this
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 & lt; AudioSource & gt; ();
myRigidBody2D = GetComponent & lt; Rigidbody2D & gt; ();
myRigidBody2D.simulated = false; // -------- add this
}
}
// Update is called once per frame
void Update()
{
if (!hasStarted)
{
LockBallToPaddle();
LaunchOnMouseClick();
}
}
private void LaunchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
myRigidBody2D.velocity = new Vector2(xPush, yPush);
myRigidBody2D.simulated = true; // -------- add this
}
}
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);
}
}
}