1
\$\begingroup\$
private void LockBallToPaddle()
{
    Vector2 paddlePos = new Vector2(Paddle.transform.position.x, Paddle.transform.position.y);
    transform.position = paddlePos + paddleToBall;   //For the ball to stick to the paddle
}

this is my coding for the ball stick to the paddle.

do i just instantiate as per normal? like instantiate("gameobject", transform.position) ?

\$\endgroup\$
1
  • \$\begingroup\$ Why destroy the ball at all? If you need a new one, would it make more sense to hide it off screen for a second or two (I assume there is some graphic effect you want to play) and then just move it back to the starting point? \$\endgroup\$ Commented Jan 15, 2021 at 8:02

1 Answer 1

1
\$\begingroup\$

There are several ways to do that, and which one is the best for you depends on how you built your game and what features you consider to add later.

Making the ball responsible for reinstating itself when destroyed might be possible by having it remember its original position in Start(), implementing its OnDestroy event and creating a clone of itself with Instantiate(gameObject, startPosition). But I would not recommend it. The reason is that it is now difficult to really destroy the ball when you actually want to get rid of it. You can do it by first disabling the script which causes the ball to respawn itself, but that's easy to forget. And you might also find yourself in circumstances where that's hard to do.

What I would prefer instead is to work with a separate gameObject with a separate script "BallSpawner". This object serves the following purposes:

  1. Represent the point in your scene where you want the ball to spawn. Having an invisible gameObject for that is useful, because you can now place it visually in your scene and even move it during the game.
  2. Spawn a ball when the game starts
  3. Check if the ball still exists (hint: variables pointing to destroyed gameObjects are considered equal to null in comparisons)
  4. Spawn another ball when it doesn't.

Untested code:

class BallSpawner : MonoBehaviour {
    public GameObject ballPrefab; 
    private GameObject currentBall;

    void Update() {
        // check if the ball does not exist yet or got destroyed
        if (currentBall == null) {
             // create a new ball at the position of the BallSpawner
             currentBall = Instantiate(ballPrefab, transform.position);
        }
    }
}

Another option would be to make the scripts which destroy the ball responsible for reinstantiating it. You can do that by just creating another instance of the ball you are just about to destroy. I assume you have some trigger-area behind each paddle which destroys the ball? Then I think it should look something like this (again, untested code):

class BallDestroyer : MonoBehaviour {

    public Vector3 ballRespawnPosition;

    void OnTriggerEnter(Collider other) {
        if (other.tag == "Ball") { // <-- always verify that the object is the one you expect!
            Instantiate(other.gameObject, ballRespawnPosition);
            Destroy(other.gameObject)
        }
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .