Question by
GeovaneUnity54 · Apr 19 at 03:05 PM ·
delay
How do I get a delay after the Player is destroyed?
What I want to do is the following:
After the character dies wait a few seconds and only after he respawns, to wait for him after a few seconds I used a coroutine, but a guy told me that a destroyed GameObject doesn't run the coroutine, so I create another code and code in the memory in the camera, in this code I did the following: If player Life = false run a coroutine. But it still didn't work.
Maybe it's not sure because as the Player is destroyed the code, an error appears that the Player is gone.
I don't know what else to do, for God's sake help me please ;-;
Here is the Player code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class VidaPlayer : MonoBehaviour
{
public bool Vivo = true;
public Image lifeBar;
public int valorAtual = 3;
public GameObject Personagem;
public GameObject Coracao;
public GameObject Coracao1;
public GameObject Coracao2;
private Renderer SP;
private Renderer SP1;
private Renderer SP2;
public ParticleSystem Explosao_Player;
public Vector3 posInicial;
public bool podeRessucitar = false;
void Start()
{
SP = Coracao.GetComponent<Renderer>();
SP1 = Coracao1.GetComponent<Renderer>();
SP2 = Coracao2.GetComponent<Renderer>();
SP.enabled = true;
SP1.enabled = true;
SP2.enabled = true;
posInicial = new Vector3(0, 0, -1);
transform.position = posInicial;
}
void Update()
{
}
void OnTriggerStay2D(Collider2D outro)
{
if(outro.gameObject.tag == "Checkpoint")
{
posInicial = outro.gameObject.transform.position;
}
else if (valorAtual == 2)
{
SP2.enabled = false;
}
else if (valorAtual == 1)
{
SP1.enabled = false;
SP2.enabled = false;
}
if(valorAtual <= 0)
{
SP.enabled = false;
SP1.enabled = false;
SP2.enabled = false;
podeRessucitar = true;
Vivo = false;
Instantiate(Explosao_Player, transform.position, Quaternion.identity);
Destroy(Personagem);
Destroy(Explosao_Player);
//StartCoroutine("TempoDeRessucitar");
}
}
void Inicializar()
{
transform.position = posInicial;
}
void Morrer()
{
Reiniciar();
Inicializar();
}
void Reiniciar()
{
SceneManager.LoadScene(0);
}
IEnumerator TempoDeRessucitar()
{
yield return new WaitForSeconds(0.5f);
Morrer();
Inicializar();
}
}
Here is the code that is in the Camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Coroutine : MonoBehaviour
{
private VidaPlayer vpc;
public GameObject Player;
void Start()
{
vpc = Player.GetComponent<VidaPlayer>();
vpc.Vivo = true;
}
// Update is called once per frame
void Update()
{
}
void Ressucitar()
{
if(vpc.Vivo == false)
{
StartCoroutine("TempoDeRessucitar");
}
}
}
Comment
Answer by alessio1918m · Apr 19 at 03:46 PM
Maybe you should call a method of an external object, something like:
public class Player : MonoBehaviour
{
[SerializeField] private GameObject something; // gameObject that has death script
private DeathScript deathScript(); // script that has death method
void Awake()
{
deathScript = something.GetComponent<DeathScript>(); // binds local deathScript to the one on the gameObject
}
void playerDeath()
{
//your code, instantiate death particles, etc...
deathScript.OnPlayerDeathMethod() //this method (from the different script) will contain the coroutine and everything else
//destroy the player
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613055555im_/https://answers.unity.com/themes/thub/images/avi.jpg)