Null Reference Exception
I'm working on a game and I've run into a problem that no matter what I try, won't be fixed. Whenever I start the game it gives me this error: NullReferenceException: Object reference not set to an instance of an object GameMaster.Awake () (at Assets/GameMaster.cs:12). And whenever I'm supposed to die it gives me this error: NullReferenceException: Object reference not set to an instance of an object GameMaster.KillPlayer (.Player player) (at Assets/GameMaster.cs:27) Player.DamagePlayer (Int32 damage) (at Assets/Players/Meelee/Scripts/Player.cs:27). Here are the two scripts I'm using: using System.Collections; using UnityEngine;
public class GameMaster : MonoBehaviour {
public static GameMaster gm;
void Awake ()
{
if(gm == null)
{
gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
}
}
public Transform playerPrefab;
public Transform spawnPoint;
public void RespawnPlayer()
{
Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
}
public static void KillPlayer(Player player)
{
Destroy(player.gameObject);
gm.RespawnPlayer();
}
} and using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour {
[System.Serializable]
public class PlayerStats
{
public int health = 100;
}
public PlayerStats playerStats = new PlayerStats();
public int fallBoundary;
void Update()
{
if (transform.position.y <= -20)
DamagePlayer(77777777); //Just to make sure it's dead... very dead.
}
public void DamagePlayer(int damage)
{
playerStats.health -= damage;
if(playerStats.health <= 0)
{
GameMaster.KillPlayer(this);
}
}
}
Thx.
Your answer
Follow this Question
Related Questions
NullReferenceException when using gameObject.SetActive(false); 0 Answers
Help needed finding NullReferenceException 1 Answer
So I know this has been asked before but, I feel this only applies to my problem. 2 Answers
NullReferenceException on a GameObject holding the main scene camera as the parent 1 Answer