Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
 }
 }
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.

Follow this Question

Answers Answers and Comments

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

Wait time Before Camera Switching? 0 Answers

Slight delay adding 0 Answers

script an delay 0 Answers

PLEASE HELP!!! 0 Answers

Hi, Im trying to make a OnTriggerStay2D but with some delay, and I dont know how is, someone can help me please? I share mi code below. 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