• 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 Timze · Apr 19, 2017 at 09:34 PM · instantiatedestroyrespawning

Respawning gameobject

 private Rigidbody2D RB2D;

 public float fallDelay;             // The time before platform falls
 public float RespawnDelay;          // The time before platform respawns
 public Transform Spawnpoint;        // Objects spawnpoint


 void Start()
 {
     RB2D = GetComponent<Rigidbody2D>();
 }

 void OnCollisionEnter2D(Collision2D col)
 {
     if (col.collider.name == ("Player"))
     {
         StartCoroutine(Fall());
     }
 }

 IEnumerator Fall()
 {        
     yield return new WaitForSeconds(fallDelay);            // Wait platform to fall
     RB2D.isKinematic = false;                              // Not kinematic so it falls
     GetComponent<Collider2D>().isTrigger = true;           // Is trigger now so won't collide
     Destroy(RB2D.gameObject, 12);                          // Destroy object after set time
     yield return new WaitForSeconds(RespawnDelay);         // Wait respawn delay
     RB2D.isKinematic = true;                               // Kinematic again so it won't fall
     GetComponent<Collider2D>().isTrigger = false;          // Is trigger again so collions apply
     Instantiate(RB2D, Spawnpoint.position, Spawnpoint.rotation);// Instantiate to the Spawnpoint
     yield return 0;
 }

What it does: There is platform on scene that when player is colliding with it, it soon falls and after set time it respawns to the spawnpoint and then is destroyed after player again collides with it.

It currently works quite nice BUT when it creates new instances, it makes clones of itself so finaly there is like "gameobject(clonecloneclone...)". Just looks stupid and there surely is better way.

Also, now I must wait before destroying the gameobject or else I can't spawn it anymore because it doesn't exists.

So, how could I make the code better? I read about disabling it only and not destroying it but couldn't get it to work...

While I copied parts of it from web, I made it also suit better my needs, and since I'm still quite new to coding I could had surely made it better. Any thoughts of it overall all welcome.

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

2 Replies

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

Answer by thaMorganic · Apr 20, 2017 at 08:04 AM

Why destroy the game object when you can just move it to the spawn point?

Use this instead of the Destroy line.

 RB2D.transform.position = Spawnpoint.transform.position;
Comment
Add comment · Show 4 · 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 Timze · Apr 20, 2017 at 10:34 AM 0
Share

Was thinking it way to hard and didn't notice that. I should be way over that allready... Now my code looks like the following:

     yield return new WaitForSeconds(fallDelay);
     RB2D.is$$anonymous$$inematic = false;
     GetComponent<Collider2D>().isTrigger = true;
     yield return new WaitForSeconds(respawnDelay);
     RB2D.is$$anonymous$$inematic = true;
     GetComponent<Collider2D>().isTrigger = false;
     RB2D.transform.position = Spawnpoint.transform.position; 
     yield return 0;

After setting RB2D.is$$anonymous$$inematic = true; again, I would assume it should stop but as it seems it just keep falling faster and faster due gravity. Ain't that supposed to set it so that it WON'T be affected by gravity?

So currently it: falls normally and won't collide with anything --> spawns back on it's position but it keep the speed it has gained from falling. How to stop it?

avatar image Maldorath · Apr 20, 2017 at 12:41 PM 1
Share

To stop it try:

RB2D.velocity = Vector2.zero

avatar image Timze Maldorath · Apr 20, 2017 at 09:52 PM 0
Share

Works fine now, thanks to both of you.

avatar image thaMorganic Maldorath · Apr 21, 2017 at 02:06 AM 0
Share

You're most welcome :)

avatar image
0

Answer by Dr_ZIZO · Apr 21, 2017 at 09:13 AM

This is bad for performance especially on mobile devices. Anyway there's a much better way that doesn't have the heavy "instantiate " : Stack.

You can declare a Stack and put your objects to it and recycle your objects instead of instantiating them every time.

The "clone" problem is really easy. After you've declared your Stack , you can declare a name to all the clones.

If your Stack name is RBs , then you can write:

 RBs.peek().name = "RB";

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Looking for gameObjects in the update function 2 Answers

Destroying all enemy characters after the level is over 4 Answers

how to destroy an object then instantiate another from a prefab 1 Answer

Instantiating droppable items on destroy game object 1 Answer

Raycast Specific Object/Instantiate Explosion 1 Answer


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