Error CS0029 - Trying to destroy an object and then instantiate it again?

I’m very new to programming and game design, so I appreciate any help thrown my way.

In designing a “Pong” style game, I’m trying to destroy the ball object after a player scores, then instantiate it again in the center of the screen, making it wait 3 seconds before moving. My code for this portion of the ball script looks like this:

        if((col.gameObject.name == "LeftGoal") || (col.gameObject.name == "RightGoal"))
        { Destroy(gameObject);
        Rigidbody2D clone;
        clone = Instantiate(rigidBody, transform.position = 0,0) as Rigidbody2D;
        clone.velocity = transform.TransformDirection(Vector2.right * speed);
        }

Thank you in advance!

The easiest way to accomplish this is to not destroy the object at all, but rather reset it. Even though it does not matter in your case, with increasing complexity, destroying and instantiating objects will more and more become something to avoid if not necessary.

So instead of your code, just set the position and speed of the ball, and trigger the don’t-move-for-3-seconds thing as well.

Your error, however, comes from this here

transform.position = 0,0

0,0 is not a literal. Instead, what C# sees here is a third parameter:

Instantiate(rigidBody,
    transform.position = 0,
    0);

Now, this term here

transform.position = 0

doesn’t work, because a position cannot be 0, since it has to be a Vector3, not a single number.

And the third parameter of Instantiate should be a Quaternion, and again, we have a 0, so that doesn’T work either.

What you apparently wanted to do is to instantiate the clone at position (0, 0). As you noticed, Instantiate takes a value for the spawnee’s position for the second parameter. A method taking a parameter value does not, however, have anything to do with assigning values. You just pass them:

Instantiate(something, Vector3.zero, ...)

The fact that Instantiate will eventually use the Vector3.zero as some object’s transform.position should be none of your concern at this point :slight_smile:

Thank you so much! Helped me out a lot.