Hello. i am currently watching the survival shooter tutorial and i was doing fine. I am following precisely the instruction and besides the fact that i am kinda new in unity , i am a bit familiar with some stuff because i already made some 2D games(with Unity). So the issue here is that the code and game was perfectly working besides some details in the animator which i fixed. I run the game and it was warking just fine. Then suddenly without doing anything, i run the game agan and it wasn’t working… the enemy was chasing but did nothing else… the error was :
NullReferenceException: Object reference not set to an instance of an object
CompleteProject.
which basically is weird because i triple checked it for hours. all the references and objects and tags and all of the stuff are fine. The code is copied from the tutorial. I feel like it’s a bug. But i really need a second “eye” on that to make sure it’s not an issue from my position.
**Error is about playerHealth.currentHealth on the if statement.
I also searched for answers and i someone suggested to Debug the playerHealth variable to see if it’s null (playerHealth = player.GetComponent ();). And it’s actually null. But i dont understand why
The code :
using UnityEngine;
using System.Collections;
namespace CompleteProject
{
public class EnemyAttack : MonoBehaviour
{
public float timeBetweenAttacks = 0.5f; // The time in seconds between each attack.
public int attackDamage = 10; // The amount of health taken away per attack.
Animator anim; // Reference to the animator component.
GameObject player; // Reference to the player GameObject.
PlayerHealth playerHealth; // Reference to the player's health.
EnemyHealth enemyHealth; // Reference to this enemy's health.
bool playerInRange; // Whether player is within the trigger collider and can be attacked.
float timer; // Timer for counting up to the next attack.
void Awake ()
{
// Setting up the references.
player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = player.GetComponent <PlayerHealth> ();
if (playerHealth == null)
{
Debug.LogError("Is Null"); // this code is execute. playerHealth is actually null
}
enemyHealth = GetComponent<EnemyHealth>();
anim = GetComponent <Animator> ();
}
void OnTriggerEnter (Collider other)
{
// If the entering collider is the player...
if(other.gameObject == player)
{
// ... the player is in range.
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
// If the exiting collider is the player...
if(other.gameObject == player)
{
// ... the player is no longer in range.
playerInRange = false;
}
}
void Update ()
{
// Add the time since Update was last called to the timer.
timer += Time.deltaTime;
// If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
{
// ... attack.
Attack ();
}
// If the player has zero or less health...
if(playerHealth.currentHealth <= 0) // object reference error here
{
// ... tell the animator the player is dead.
anim.SetTrigger ("PlayerDead");
}
}
void Attack ()
{
// Reset the timer.
timer = 0f;
// If the player has health to lose...
if(playerHealth.currentHealth > 0) // object reference error here
{
// ... damage the player.
playerHealth.TakeDamage (attackDamage);
}
}
}
}