Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SleepingWillowGames · Jul 31, 2017 at 09:42 PM · instantiateclonedestroy object

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SleepingWillowGames · Jul 31, 2017 at 10:03 PM 0
Share

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.

avatar image FlaSh-G · Jul 31, 2017 at 10:15 PM 1
Share

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.

avatar image SleepingWillowGames · Jul 31, 2017 at 10:27 PM 0
Share

Thank you so much! Helped me out a lot.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Following Object can't find newly Instantiated Object (Solved) 1 Answer

How can I assign a clone with a script to a variable? without drag & drop 1 Answer

my object instantiates to much 1 Answer

Moving a transform behaves odd 0 Answers

Deep clone a gameobject 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges