Instantiate ball breaks update

block-breaker
#1

I’m trying to add more lives by instantiating a new ball when it falls down. However, once I instantiate it I noticed it doesn’t run update anymore. I made it work with resetting the position but that does feel a bit hacky. So I’d like to know what is going on.
lose collider was calling the following method on the paddle. It created a ball clone but it didn’t update.

    public void NewBall()
    {
        Vector2 startPostion = new Vector2(7.716f, transform.position.y);
        Instantiate(ball, startPostion, Quaternion.identity);
    }
#2

Hi Arjen,

The new ball needs to be assigned to the paddle.

Before you start challenging yourself, is there a reason why you do not reuse the old ball? You don’t have to destroy it.

#3

I did fix it with a transform position for now but in the future I might want to generate more then one ball.
I’m not getting a null btw reference and I see a paddle on the cloned ball.

The below code is in both void start and void awake. I even tried deleting one method and leaving the other.

       hasStarted = false;
        myAudioSource = GetComponent<AudioSource>();
        myRigidBody2D = GetComponent<Rigidbody2D>();
        paddle1 = FindObjectOfType<Paddle>();
        paddleToBallVector = transform.position - paddle1.transform.position;

The reason I think my void update is the problem is that I had a Debug.Log(“running”) in it that stopped once the ball was cloned.

#4

Well done! :slight_smile:


See also:

#5

Thanks but I’m still unsure what I’m doing wrong with my instantiate

#6

What do you mean by wrong? Do you get any error messages? When you instantiate a new ball, you need to assign the things you assigned manually in Unity to your former ball via code.

#7

I had a Debug.Log(“running”) in update, Debug.Log(“start”) in the start and Debug.Log(“new”) in my awake.

With a new game I would get start and then a million times running. After I lost the ball I got a new but I wouldnt get running anymore.

#8

Is the new ball active in your Hierarchy? Maybe something happened to the new ball after its Start() method had been called.

#9


There is a new ball in the hierarchy but it isn’t updating.

#10

What is “ball (clone)” and why are the Circle Collider 2D and Ball components disabled?

#11

the ball(clone) is what is instantiated. Not sure why it is disabled. The prefab has it enabled.

#12

Double click on the red error message. There is a variable named collider1. In which line of your code is it?

#13

I can’t reproduce that error. I did update the prefab and added the paddle to it. It is linked now but for some reason upon creation the collider and the script are turned off. This is how I call the ball now btw.

public void NewBall()
    {
        Vector2 startPostion = new Vector2(transform.position.x + 7.716f, transform.position.y);
        Ball newball = Instantiate(ball, startPostion, Quaternion.identity);
    }

#14

I did upload my current game btw. If we can find the solution now good if not I’ll get back to it later when I have some more experience. I couldn’t get sharemygames to work so it’s on itch.io
https://alphabravoab.itch.io/bouncer