Instantiate new Ball issue

Hi I’m trying to Instantiate a new ball into my scene so that when the player dies, he loses 1 life and the ball comes back to the paddle. Much like re-loading the scene except keeping the already broken bricks broken so the player doesn’t have to break them over again.

So far I have destroyed the old ball and instantiated a new one at a set position, but I don’t know how to reattached it to the paddle. When I Instantiate the new ball, it just floats at the position where I instantiated instead of attaching to or moving with the paddle, which I seems like it should be done during update in Ball. The ball does launch on click, so I’m doing something right at least.

Here’s the code I’ve added to GameSesssion:

public int LoseALife()
{
    lives--;
    lifeText.text = lives.ToString();
    ResetBall();
    return lives;
}

private void ResetBall()
{
    Ball oldBall = FindObjectOfType<Ball>();
    oldBall.DestroyBall();
    Instantiate(newBall, new Vector3(5, 1.25f, 3), transform.rotation);
    Ball ball = FindObjectOfType<Ball>();
    ball.ResetPosition();

And what I’ve added to Ball (plus Update and LockBallToPaddle, for reference):

void Update()
{
    if (!hasStarted)
    {
        LockBallToPaddle();
        LaunchOnMouseClick();
    }
}
private void LockBallToPaddle()
{
    Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    transform.position = paddlePos + paddleToBallVector;
}
public void ResetPosition()
{
    hasStarted = false;
    print ("Resetting");
    paddleToBallVector = transform.position - paddle1.transform.position;
}
public void DestroyBall()
{
    Destroy(gameObject);
}

I’m also wondering if using Ball oldBall = FindObjectOfType(); is the best way to find the ball. If I were to add extra balls at once, is there an easy way to find which ball is triggering the Lose Collider, or would I need to do something like FindObjectsOfType(); and search through all balls to find which has a y position <0?

Thanks you.

Hi Justin,

Why do you destroy the old ball instead of simply reusing it? When you set hasStarted to false again, the ball should teleport to its position above the paddle.

Do you have a NullReferenceException in your console when you instantiate a new ball? NullReferenceException means that a reference (“link”) to an instance is missing. If you instantiate a new ball, it must know the Paddle object.

Thanks Nina, just calling ResetPosition() on the old ball solved the issue.

I think I got the NullReferenceException the first time, but I attached the paddle to the ball prefab, and that worked.

I haven’t added additional balls yet so we’ll see if I can do that successfully.

I think I got the NullReferenceException the first time, but I attached the paddle to the ball prefab, and that worked.

Unity usually complains if you assign an object from the Hierarchy to a prefab. If you attached the paddle prefab, it might be that you didn’t get a NullReferenceException. However, the paddle prefab doesn’t have anything to do with the paddle in your scene while running your game.

Hmm, gotcha, that’s why the new instantiated ball wouldn’t stick. So if I want to make extra balls I should make a new ball prefab which doesn’t need a paddle assigned?

The prefab should have a script attached that looks for a Paddle instance in its Awake or Start method. Since you have only one Paddle instance in your scene, you could use FindObjectOfType<Paddle>(). When the prefab gets instantiated the Awake and Start method of the new object get called.

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

Privacy & Terms