The question is answered, right answer was accepted
Null Reference Exception Error
Hi,
I wrote two scripts to handle health in my game. One for my player to have Health and one for enemies to deal damage to my player.
In my DealDamage function it seems that Unity can't find the playerHealth.currentHealth call. It only gives back a Null Reference Exception Error.
I can not find out what I did wrong. :(
My PlayerHealth Script:
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
public int startingHealth;
public int currentHealth;
public GameObject playerExplosion;
private GameController gameController;
private bool isDead;
void Start()
{
isDead = false;
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject !=null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if(gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
}
void Awake()
{
currentHealth = startingHealth;
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0 && !isDead)
{
Death ();
}
}
void Death()
{
isDead = true;
Destroy (gameObject);
Instantiate (playerExplosion, transform.position, transform.rotation);
gameController.GameOver();
}
}
and my EnemyHit Script:
using UnityEngine;
using System.Collections;
public class EnemyHit : MonoBehaviour
{
public int attackDamage;
GameObject player;
PlayerHealth playerHealth;
void Start()
{
player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = player.GetComponent<PlayerHealth> ();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
DealDamage ();
}
}
void DealDamage()
{
if (playerHealth.currentHealth > 0)
{
playerHealth.TakeDamage (attackDamage);
}
}
}
It works fine here!
Player is tagged as "Player" ?
PlayerHealth script is attached to Player ?
EnemyHit is attached to Enemy, Weapon or Projectile... ?
Yes Player is tagged as Player.
Yes it is.
EnemyHit is attached to Enemy. (Enemy shall bump into my Player)
what line is the error on? that error simply means you are probobly trying to ask about an empty variable such as a gameobject variable.
In line 28 of the EnemyHit script where I ask wether the current health of the player is bigger than one.
Answer by WillNode · Mar 15, 2016 at 01:45 AM
I can guess this:
Your EnemyHit is disabled while the GameObject (who attaching EnemyHit), which prevent EnemyHit's Start function be called first. please note that according to the docs, OnTriggerEnter still be called even your script is disabled, so to avoid headaches, make sure you have EnemyHit enabled or....
void DealDamage() { if(!playerHealth) { Start(); if(!playerHealth) Debug.LogError("PlayerHealth not found, go to solution 2"); } if (playerHealth.currentHealth > 0) { playerHealth.TakeDamage (attackDamage); } }
Make sure that ONLY your player has tag "player", unfortunely, there is no quick solution for this*, so you need to iterate/check every of gameobject in your scene whenever any other gameobject accidentally tagged as "player". or see a (not quick) solution here
PS : This can be a good Feature request :)
Okay, I tried that. The debug log appeared so Unity did not find PlayerHealth, but solution 2 was negative. I didn't find any GameObject tagged as "Player" except of the Player GameObject itself.
One thing could be I've a ChildObject attached to the player which holdes the $$anonymous$$esh Filter, the $$anonymous$$esh Renderer and the $$anonymous$$esh Collider on it. This ChildObject is tagged as player too... could this cause the Error?
Solved it! $$anonymous$$y problem was, that I attached the Collider of my player to the ChildObject and this Object dind't have the PlayerHealth script attached to it so there was no way for the Enemy and Player to bump into each other. The Enemy bumps into the Collider of the player's ChildObject but there is no PlayerHealth attached to it so no damage at all.
I put the collider on the Player GameObject where the PlayerHealth script is also attached to and see it works.
Thank you very much for your help! :)
Follow this Question
Related Questions
Object reference not set to an instance of an Object C# 2 Answers
I really need help with this message 0 Answers
UI null reference errors in script, but not in inspector. 1 Answer
Object reference not set to an instance of an object [HELP!] 0 Answers
PLEASE help, the Anim.SetBool or Anim.SetFloat s are sending tons of errors! 1 Answer