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. Dismiss Notice

My Player Doesn't Resurrect

Discussion in 'Scripting' started by GeovaneUnity54, May 26, 2022.

  1. GeovaneUnity54

    GeovaneUnity54

    Joined:
    Nov 14, 2021
    Posts:
    91
    Good morning guys, so, I was wanting to make the Player resurrect after 1.5f, so I made the following code and put it in the camera:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Camera2D : MonoBehaviour {
    4.    private Vector2 velocity;
    5.    public float smoothTimeY;
    6.    public float smoothTimeX;
    7.    public float deltaY;
    8.  
    9.    public GameObject player;
    10.    public Transform SpawnPlayer;
    11.    void Start(){
    12.       if(player != null)
    13.       {
    14.          player = GameObject.FindGameObjectWithTag("Player");
    15.          smoothTimeY = 0.2f;
    16.          smoothTimeX = 0.2f;
    17.          deltaY = 0.4f;
    18.       }
    19.    }
    20.    void FixedUpdate(){
    21.       if(player != null)
    22.       {
    23.          float posX = Mathf.SmoothDamp(transform.position.x, player.transform.position.x, ref velocity.x, smoothTimeX);
    24.          float posY = Mathf.SmoothDamp(transform.position.y, player.transform.position.y + deltaY, ref velocity.y, smoothTimeY);
    25.          transform.position = new Vector3(posX, posY, transform.position.z);
    26.       }
    27.    }
    28.  
    29.    void Ressucitar()
    30.    {
    31.       if(player == null)
    32.       {
    33.          StartCoroutine("RessucitarPlayer");
    34.       }
    35.    }
    36.  
    37.    IEnumerator RessucitarPlayer()
    38.     {
    39.       yield return new WaitForSeconds(1.5f);
    40.       Instantiate(player, SpawnPlayer.position, transform.rotation);
    41.     }
    42. }
    When I start the Game and let the Player die, the Player reference I had made in the Inspector disappears, and the camera is like "What object do I instantiate? Where is it missing?" But I don't know how to solve.

    Thanks in advance.
     
  2. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    324
    Hey mate, when your line 14 run, you are saying "Hey camera, gimme the reference to the first object that you find IN THE CURRENT SCENE that has the tag Player", but when you kill your player (unless that by KILL you mean deactive and activate the game object) this reference isn't valid anymore, cause the reference get on start is now gone. What you need is to reference the prefab instance of the player and not the runtime instance of player, just remove the find object and manually add the prefab that things should work.
     
  3. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hello,

    My guess is that you destroy the player when it dies is that right ?

    In which case, the "player" reference you got with "player = GameObject.FindGameObjectWithTag("Player");" in your Start() is now null since you deleted that object.

    instead, adds another field "public GameObject playerPrefab;" that you assign in the editor to the correct prefab, and use this one to instantiate the player when resurecting.