2D Platforme NullReferenceException #12 respawn error [SOLVED]

Hello,
I’ve look into the web, but i didn’t find any answer to my error. I watch

When my Char die, it cannot respawn due toan error :

NullReferenceException: Object reference not set to an instance of an object
Player.DamagePlayer (Int32 damage) (at Assets/Player.cs:31)
Player.Update () (at Assets/Player.cs:21)

Here’s my code for the file Player.cs :

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

[System.Serializable]
public class PlayerStats
{
public int Health = 100;

}

public PlayerStats playerStats = new PlayerStats();

public int fallBoundary = -20;

void Update()
{
if (transform.position.y <= fallBoundary)
{
DamagePlayer(999999);
}
}


public void DamagePlayer(int damage)
{
playerStats.Health -= damage;
if (playerStats.Health <= 0)
{
GameObject.FindGameObjectWithTag("Player").GetComponent<GameMaster>().KillPlayer(this);
}
}

}

Really need help about that, i don t understand. Moreover c# is not really my best.

Thx for helping guys.

The line that it is struggling with is:

    GameObject.FindGameObjectWithTag("Player").GetComponent<GameMaster>().KillPlayer(this);

There could be several reasons for this, including:

  1. your player does not have the “Player” tag.
    2).your player does not have the GameMaster script attached to it
    3). there is more than one object with the player tag

Well, thx for the response !

You were right, i’ve more than one player tag, so i fiex the issue.

But now i’ve another error :

MissingReferenceException: The object of type ‘Transform’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:63)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:80)
GameMaster+c__Iterator0.MoveNext () (at Assets/GameMaster.cs:26)

and here is the code of the file GameMaster.cs

using UnityEngine;
using System.Collections;

public class GameMaster : MonoBehaviour {

public static GameMaster gm;

void Start()
{
    if (gm == null)
    {
        gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
    }
}

public Transform playerPrefab;
public Transform spawnPoint;
public int spawnDelay = 2;
public IEnumerator RespawnPlayer()
{
    Debug.Log("TODO: add waiting for spawn sound");
    yield return new WaitForSeconds(spawnDelay);
    Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
    Debug.Log("TODO: add Spawn Particles");
}

public void KillPlayer(Player player)
{
    Destroy(player.gameObject);
    gm.StartCoroutine(gm.RespawnPlayer());
}

}

thx again :smiley: