Hey guys I’m fairly new to unity and was working on a project for school and came across this error
The Type or namespace name ‘EnemyHealth’ could not be found(are you missing a using directive or an assembly reference?)
All I’m trying to do is call the take damage function in my enemy health code.
Here is my code:
using UnityEngine;
using System.Collections;
public class PLayerShooting : MonoBehaviour
{
public ParticleSystem muzzleFlash;
Animator anim;
public GameObject impactPrefab;
public float hitForce = 1000;
int amount = 1;
bool shooting = false;
GameObject target;
// Use this for initialization
void Start ()
{
anim = GetComponentInChildren<Animator>();
}
// Update is called once per frame
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
muzzleFlash.Play();
anim.SetTrigger("Fire");
Shooting();
}
}
void Shooting()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 50f))
{
EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth>();
if (enemyHealth != null)
{
enemyHealth.TakeDamage(amount);
}
Instantiate(impactPrefab, hit.point, transform.rotation);
impactPrefab.transform.position = hit.point;
impactPrefab.GetComponentInChildren<ParticleSystem>().Play();
}
}
}
Hey guys i managed to solve the problem it was a matter of my scripts not being in the same script folder. I thank you JedBeryll for the quick response its good to see people interested in helping newcomers like me.
Class is different from ‘namespace’, However, you can still do what you’re trying to do. In PlayerShooting class do this: Add EnemyHealth enemyHealth; as a constant(meaning before any functions), then you should be fine. @exploseis5