What should be happening is “Game Over” appearing upon player death, but when the script is called, it tells me theres no reference to an object. But i have it set in the inspector as a text element. And none of the scripts return a bug, so everything is “correct.” This is the error I’m getting exactly. I’ll reference what the trouble lines are with comments.
NullReferenceException: Object reference not set to an instance of an object
playerHealth.Death () (at Assets/Scripts/playerHealth.cs:86)
playerHealth.TakeDamage (Int32 amount) (at Assets/Scripts/playerHealth.cs:77)
AsteroidDmgMed.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/AsteroidDmgMed.cs:23)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public Vector3 spawnValue;
public GameObject[] hazard;
public int hazardCount;
public float spawnWait;
public float waveWait;
public float startWait;
int randHazard;
public Text gameOverText;
public Text restartText;
private bool gameOver;
private bool restart;
void Start ()
{
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
StartCoroutine (SpawnWaves ());
}
void Update ()
{
if (restart) {
if (Input.GetKeyDown (KeyCode.R)) {
UnityEngine.SceneManagement.SceneManager.LoadScene ("Main");
}
}
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true) {
for (int i = 0; i < hazardCount; i++) {
randHazard = Random.Range (0, 3);
Vector3 spawnPositionXpos = new Vector3 (spawnValue.x, Random.Range (-spawnValue.y, spawnValue.y), spawnValue.z);
Vector3 spawnPositionXneg = new Vector3 (-spawnValue.x, Random.Range (-spawnValue.y, spawnValue.y), spawnValue.z);
Vector3 spawnPositionYpos = new Vector3 (Random.Range (-spawnValue.x, spawnValue.x), spawnValue.y, spawnValue.z);
Vector3 spawnPositionYneg = new Vector3 (Random.Range (-spawnValue.x, spawnValue.x), -spawnValue.y, spawnValue.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard [randHazard], spawnPositionXpos, spawnRotation);
Instantiate (hazard [randHazard], spawnPositionXneg, spawnRotation);
Instantiate (hazard [randHazard], spawnPositionYpos, spawnRotation);
Instantiate (hazard [randHazard], spawnPositionYneg, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
if (gameOver) {
restartText.text = "Press 'R' to Restart";
restart = true;
break;
}
}
}
public void GameOver ()
{
gameOverText.text = "Game Over";
gameOver = true;
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class playerHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public Slider healthSlider;
public Image damageImage;
public Image healImage;
public float DmgflashSpeed = 5f;
public float HealflashSpeed = 5f;
public Color DmgColor = new Color(1f, 0f, 0f, 0.1f);
public Color HealColor = new Color (0f, 1f, 0f, 0.1f);
playercontroller playerMovement;
GameController controller;
bool isDead;
bool damaged;
bool healed;
void Awake ()
{
controller = GetComponent <GameController> ();
playerMovement = GetComponent <playercontroller> ();
currentHealth = startingHealth;
}
void Update ()
{
if(damaged)
{
damageImage.color = DmgColor;
}
else
{
damageImage.color = Color.Lerp (damageImage.color, Color.clear, DmgflashSpeed * Time.deltaTime);
}
damaged = false;
if (healed) {
healImage.color = HealColor;
}
else {
healImage.color = Color.Lerp (healImage.color, Color.clear, HealflashSpeed * Time.deltaTime);
}
healed = false;
}
public void HealDamage (int amount)
{
if (currentHealth == 100) {
healed = false;
}
if (currentHealth < 100) {
healed = true;
currentHealth += amount;
healthSlider.value = currentHealth;
}
}
public void TakeDamage (int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
if(currentHealth <= 0 && !isDead)
{
Death (); //Line 77
}
}
public void Death ()
{
isDead = true;
playerMovement.enabled = false;
controller.GameOver (); //Line 86
}
}
using UnityEngine;
using System.Collections;
public class AsteroidDmgMed : MonoBehaviour {
public int asteroidMedDmg = 10;
GameObject player;
playerHealth PlayerHealth;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player");
PlayerHealth = player.GetComponent <playerHealth> ();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject == player)
{
PlayerHealth.TakeDamage (asteroidMedDmg); //Line 23
}
}
}