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
Is there any way to return destroyed items back to scene ? Or I have to use something else?
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.
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.
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
Okay, then I would suggest the following:
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.
Order all platforms under a parent named "DestructableItems$$anonymous$$anager"
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
Follow this Question
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