respawn player prefab issue

Hello

Please advice.

I am trying to resspawn my player.
I have setup a spawn point and my player is a prefab.

I get two errors in my console when the player health gets below 0.

The first one is obvious but i don’t know how to fix it. Its because i have a cam script which follows the player, so i guess that gets destroyed to.

  • The variable of cam of playerscript has not been assigned.
  • Null reference exception: Object reference not set to instance of object playerHealth cs.86
else if (currentHealth <= 0)
        {
            //.......................................
            lifes.numLives -= 1;
            //.............
           
         
            mySound.PlayOneShot(playerDeadAudio, 1);
            this.enabled = false;

            anim.Play("Dead");

            this.GetComponent<playerScript>().enabled = false;

            reSpawn();
             Destroy(gameObject, 5f);
          
        }

public void reSpawn()     {         Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);         //transform.position = spawnPoint.position;         currentHealth = health;         healthBar.value = currentHealth;         this.GetComponent<playerScript>().enabled = true;     }

    }

And here code for camera which is attached to the camera:

public class cameraFollow : MonoBehaviour
{

    public GameObject Target;
    private Vector3 Offset;


    // Start is called before the first frame update
    void Start()
    {
        Offset = transform.position - Target.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Target.transform.position + Offset;
    }
}

Do you absolutely have to destroy the player object when the player dies in the context of your game?

2 Likes

No I can go by with a reload level