- Home /
The type or namespace name PlayerHealth could not be found (are you missing a using directive or an assembly reference
Hi so I'm just started using unity and saw a tutorial on youtube on how to make a zombie FPS and when I use PlayerHealth for an enemy attack it gives me the error in the title. Here's my code,
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour
{
public float maxDistance;
public float coolDownTimer;
public PlayerHealth ph;
public int damage;
private Transform myTransform;
public Transform target;
// Use this for initialization
void Start()
{
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
myTransform = transform;
maxDistance = 3;
coolDownTimer = 0;
damage = -10;
ph = (PlayerHealth)go.GetComponent(typeof(PlayerHealth));
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, myTransform.position);
if (distance < maxDistance)
{
Attack();
}
if (coolDownTimer > 0)
{
coolDownTimer = coolDownTimer - Time.deltaTime;
}
if (coolDownTimer < 0)
{
coolDownTimer = 0;
}
}
void Attack()
{
Vector3 dir = Vector3.Normalize(target.position - myTransform.position);
float direction = Vector3.Dot(dir, transform.forward);
if (direction > 0)
{
if (coolDownTimer == 0)
{
ph.ChangeHealth(damage);
coolDownTimer = 2;
}
}
}
}
You need a class "PlayerHealth" somewhere in your assets. Note the the filename also needs to be PlayerHealth.
Answer by JoeriVDE · Apr 29, 2016 at 11:54 AM
First check if you have a class PlayerHealth. If you do: locate the file, check if the filename is PlayerHealth as well. If not change it to the class name. If after that the problem still occurs -> check if the class is wrapped inside a namespace, something like this:
//This is the namespace name
namespace HealthClasses
{
public class PlayerHealth // Your class
{
//Whatever's inside your class
}
}
If it is then you add 'using (namespace name);' at the top of the EnemyAttack class. using HealthClasses in this example.
Also:
//instead of writing :
ph = (PlayerHealth)go.GetComponent(typeof(PlayerHealth));
//you can simply write this:
ph = go.GetComponent<PlayerHealth>();
It does the same thing.
Hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220612075822im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how do I edit enemy stats 3 Answers
hit delay does not kick in 0 Answers
The name `target' does not exist in the current context 1 Answer