- Home /
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!
Answer by FlaSh-G · Jul 31, 2017 at 09:53 PM
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 :)
Thanks for the quick response!
So in the case of just resetting the object, what would be the easiest method to go about that in code? Sorry, like I said, I'm very new to this.
The easiest way is to just set all the values to default one by one.
You could create a method like this:
public void ResetBall()
{
transform.position = Vector2.zero;
rigidBody.velocity = Vector2.right * speed;
StartCoroutine(Disable$$anonymous$$ovementForSeconds(3));
}
private IEnumerator Disable$$anonymous$$ovementForSeconds(float seconds)
{
rigidBody.is$$anonymous$$inematic = true;
yield return new WaitForSeconds(seconds);
rigidBody.is$$anonymous$$inematic = false;
}
Don't be too confused about this StartCoroutine / IEnumerator / yield return stuff :) Disable$$anonymous$$ovementForSeconds is a "coroutine", a method that can be paused for an arbitrary amount of frames. The WaitForSeconds line basically does exactly that before re-activating the rigidbody. Coroutines always have IEnumerator as return type and must be put into a StartCoroutine call to work, like in the code.
Anyway... Just call ResetBall in the body of the if statement in your question's code.