Nightmares Shooter tutorial - 6 of 10 - Player Health Problem

Hi there, I’m really new to Unity, I just made like 5 tutorials and while making this one I stumbled upon problem which I cannot figure out.

At the end of phase 6 I get this error:

Here are the settings for the player:

I understand that the currentHealt variable is not set to correct Object, or something like that, but I don’t understand how is that possible.

I even copied the code from the tutorial page, so there aren’t any typos. Player has “Player tag”
and “Default Layer”. Zombunny has “Untagged tag” and “Shootable layer”.

I tried to debug the problem, with my limited abilities.

player = GameObject.FindGameObjectWithTag (“Player”);
this returns Player, so the correct object.

playerHealth = player.GetComponent ();
and this returns nothing. So the player variable references to correct Player object, which has the PlayerHealth script (with correct name PlayerHealth). This script has public class PlayerHealth, with public variable currentHealth, which changes in public method (or is it just a funciton in C#?) TakeDamage. Yet it doesn’t return the value of currentHealth from Player object.

I really don’t understand what’s wrong and any help would be greatly appreciated. Thank you in advance.

I ran into the same error, and the problem in my case was that there was another object in my hierarchy tagged “Player”. Specifically, the top level player object has a child also named “Player”, and somehow both ended up tagged.

Since the PlayerHealth script is attached only to the parent, the line

playerHealth = player.GetComponent <PlayerHealth> ();

runs the risk of coming back null if the preceding line

player = GameObject.FindGameObjectWithTag ("Player");

pulls the child object instead of the parent.

I solved this.

Use

player = GameObject.Find (“Player”);

instead of

player = GameObject.FindGameObjectWithTag (“Player”);

Hey!

Looking at your code here using UnityEngine;using System.Collections;public class EnemyAttack : Mo - Pastebin.com

In Awake () you have:

playerHealth = player.GetComponent ();

playerHealth.currentHealth;

Debug.Log ("His health? " + playerHealth);

The playerHealth variable doesn’t actually contain the player’s health as a number. It’s a reference to the PlayerHealth component attached to the player. When you need to see the player’s health, you would use playerHealth.currentHealth - this is like asking the PlayerHealth component for its currentHealth variable, which is an integer containing the player’s health as a number.

So, get change those three lines to just two:

playerHealth = player.GetComponent ();

Debug.Log ("His health? " + playerHealth.currentHealth);

And you should be able to see His health? and the number in the console.

From there, wherever you need to get at that number containing player health, use playerHealth.currentHealth.

Hope that makes sense!
Jeff.

That didn’t help either.

I think we don’t understand each other.

There seems to be a problem with scope or something. The EnemyAttack public class has access to Player object, but has no access to PlayerHealth public class, therefore, it cannot access the currentHealth variable.

At least that’s my limited understanding of the problem.

@robertbu thank you, I’ll certainly start doing that!