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 /
  • Help Room /
avatar image
0
Question by Agonir · Feb 23, 2017 at 03:43 PM · unity52d-platformercoroutine

Unity 2D can't resawn because object is destroyed.

Hello I want to make a 1x1 platform which when I jump on it it will destroy after certain time I have as an integer ,now I also have a respawn script to respawn all the stuff when my player dies but when I put the respawn script on my 1x1 platform it won't respawn because the object is destroyed

I can't figure how to make a coroutine for setActive(false)/(true) when I jump on platform because the item get's destroyed. Here's my destroystuff script:

 using UnityEngine;
 using System.Collections;
 
 public class DestroyItem : MonoBehaviour {
 
     public float timeToDestroy;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update ()
     {
         
         
     }
 
         
     void OnTriggerEnter2D(Collider2D other) {
         if (other.tag == "Player") {
             Destroy (gameObject, timeToDestroy);
             Debug.Log ("Item destroyed");
         }
 
     }
         
 }

Here's an GIF of my game. GIF

Comment
Add comment · Show 2
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 Agonir · Feb 23, 2017 at 03:45 PM 0
Share

Is there any way to return destroyed items back to scene ? Or I have to use something else?

avatar image jwulf Agonir · Feb 23, 2017 at 03:51 PM 0
Share

It is indeed impossible to return destroyed gameobjects. But in your case you seem to only need SetActive(), no need to destroy anything. See my answer for details.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jwulf · Feb 23, 2017 at 03:50 PM

Without your respawn-Script it's hard to tell what the mistake is, but I guess you want something like the following:

 IEnumerator DisappearAndReappear(float timeUntilDisappear, float timeUntilRespawn) {
     yield return new WaitForSeconds(timeUntilDisappear);
     gameObject.SetActive(false);
     yield return new WaitForSeconds(timeUntilRespawn);
     gameObject.SetActive(true);
 }

So if you change your OnTriggerEnter2D-Callback to:

 void OnTriggerEnter2D(Collider2D other) {
          if (other.tag == "Player") {
              StartCoroutine(DisappearAndReappear(2, 3);
          }
 }

, the platform will disappear 2 seconds after the player entered the trigger, and then 3 seconds later reappear.

Is that what you wanted?

If on the other hand you want to call the reappearance manually, you can do:

 IEnumerator DisappearAfter(float timeUntilDisappear) {
     yield return new WaitForSeconds(timeUntilDisappear);
     gameObject.SetActive(false);
 }

 void OnTriggerEnter2D(Collider2D other) {
          if (other.tag == "Player") {
              StartCoroutine(DisappearAfter(2);
          }
 }

 public void Respawn() {
     gameObject.SetActive(true);
 }

And call the Respawn()-Method from wherever you want. (There is actually no need for this method because gameObject.SetActive() is public anyway; but in my opinion this encapsulation makes sense, in case you later want to add any logic to the respawning-process.

Comment
Add comment · Show 2 · 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 Agonir · Feb 23, 2017 at 03:56 PM 0
Share

They do disappear but won't reappear ,my respawn script is as follows:

 using UnityEngine;
 using System.Collections;
 
 public class ResetRespawn : $$anonymous$$onoBehaviour {
 
     private Vector3 startPosition;
     private Quaternion startRotation;
     private Vector3 startLocalScale;
 
     private Rigidbody2D playerBody;
 
     // Use this for initialization
     void Start () {
         startPosition = transform.position;
         startRotation = transform.rotation;
         startLocalScale = transform.localScale;
 
         if (GetComponent < Rigidbody2D> () != null) {
             playerBody = GetComponent<Rigidbody2D> ();
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     public void ResetObject(){
         transform.position = startPosition;
         transform.rotation = startRotation;
         transform.localScale = startLocalScale;
 
         if (playerBody != null) {
             playerBody.velocity = Vector3.zero;
         }
 
     }
 }
 


Basically I want the platforms to get back at scene when I die

avatar image jwulf Agonir · Feb 23, 2017 at 04:02 PM 0
Share

Okay, then I would suggest the following:

  1. Give all Platforms a script like my second suggestion (note that in that script you never destroy anything, just use SetActive(false) to make the Platform disappear.

  2. Order all platforms under a parent named "DestructableItems$$anonymous$$anager"

  3. Give that one a Script with the following method:

    public void RespawnDestructables() { foreach(DestroyItem i in GetComponentsInChildren()) { i.Respawn(); } }

Now whenever you die, you just have to call RespawnDestructables() on that manager-object.

Does that help you?

EDIT: Sorry, for some reason code-formatting doesn't work in this comment. No idea why...

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

103 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 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

Unity coroutine movement over time is not consistent/accurate? 1 Answer

yield return StartCoroutine() doesnt wait for coroutine to finish 5 Answers

(2D) Make a GameObject Shake 0 Answers

Bullet destroy 2 game object intent one Unity 2D 0 Answers

NavMeshAgent dont find the right way on runtime build NavMesh 0 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