Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Destroy Player while waiting for Player to respawn?

Discussion in '2D' started by AEBlob, Mar 12, 2016.

  1. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Code (CSharp):
    1.     public IEnumerator RespawnPlayerCo()
    2.     {
    3.  
    4.         player.enabled = false;
    5.         player.GetComponent<SpriteRenderer>().enabled = false;
    6.         gravityStore = player.GetComponent<Rigidbody2D>().gravityScale;
    7.         player.GetComponent<Rigidbody2D>().gravityScale = 0f;
    8.         player.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    9.  
    10.         iceDeathPrefab = Instantiate(iceDeathPrefab);
    11.         iceDeathPrefab.transform.position = player.transform.position;
    12.         iceDeathPrefab.transform.rotation = player.transform.rotation;
    13.  
    14.         Debug.Log("Player Respawn");
    15.         yield return new WaitForSeconds(respawnDelay);
    16.  
    17.         Destroy(iceDeathPrefab);
    18.  
    19.         player.GetComponent<Rigidbody2D>().gravityScale = gravityStore;
    20.         player.transform.position = currentCheckpoint.transform.position;
    21.         player.enabled = true;
    22.         player.GetComponent<Renderer>().enabled = true;
    23.  
    24.     }
    How do I get my Player to "cease to exist", so to speak, in that period between death and respawn? At the moment he is disabled, but he remains visible and is pushed by the enemy until he respawns. This at the same time as the "death sprite" is visible. Not sure if I explained that clearly.

    My understanding is it is a bad idea to Destroy the Player object. The death sprite works fine, is instantiated and destroyed appropriately.
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If you just want him to be there but be invisible you could disable the renderer & then turn it back on again.
     
  3. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Code (CSharp):
    1.     public IEnumerator RespawnPlayerCo()
    2.     {
    3.  
    4.         player.enabled = false;
    5.         player.GetComponent<Renderer>().enabled = false;
    6.         //player.GetComponent<Animator>().enabled = false;
    7.         gravityStore = player.GetComponent<Rigidbody2D>().gravityScale;
    8.         player.GetComponent<Rigidbody2D>().gravityScale = 0f;
    9.         player.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    10.         //player.GetComponent<Animator>().enabled = false;
    11.  
    12.         iceDeathPrefab = Instantiate(iceDeathPrefab);
    13.         iceDeathPrefab.transform.position = player.transform.position;
    14.         iceDeathPrefab.transform.rotation = player.transform.rotation;
    15.  
    16.         Debug.Log("Player Respawn");
    17.         yield return new WaitForSeconds(respawnDelay);
    18.  
    19.         Destroy(iceDeathPrefab);
    20.  
    21.         player.GetComponent<Rigidbody2D>().gravityScale = gravityStore;
    22.         player.transform.position = currentCheckpoint.transform.position;
    23.         player.enabled = true;
    24.         player.GetComponent<Renderer>().enabled = true;
    25.  
    26.     }
    I tried, it didn't work for some reason.
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    What is the player.enabled? If you disable the player then nothing would work after that.
     
  5. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    It's the Players script.
     
  6. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Does player have Animator that changes the SpriteRenderer.Enabled in any of the frames?
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I don't think player is your script, I think it is your game object because a script doesn't have a rigidbody2d, in which case once you disable your game object you can't do anything to it because it doesn't exist anymore.
     
  8. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    You can do alot of things to a disabled GameObject. You can't Find() it, but if you got the reference safe, you can use it normal way. Would be different if the GameObject was be destroyed. The code he shows only disables renderer component. I've seen animations change those values at unwanted times before, even if it's some old animation that is not being used.