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

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.