- Home /
NameSpace Error EnemyHealth Cant Access Other Script.
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();
}
}
}
We need to take a look at the EnemyHealth script as well. (It's not inside a namespace is it?)
$$anonymous$$y bad here is the EnemyHealth script. I don't believe it is in a namespace or at least i didn't intend for it to be
using UnityEngine; using System.Collections;
public class EnemyHealth : $$anonymous$$onoBehaviour { public int startingHealth = 1;
public int currentHealth; public int scoreValue; private GameController gameController;
void Awake()
{
currentHealth = startingHealth;
}
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
Death();
}
}
void Death()
{
gameController.AddScore(scoreValue);
Destroy(gameObject);
}
}
Answer by exploseis5 · Dec 08, 2016 at 05:50 PM
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.
Answer by SoraMahiro · Dec 08, 2016 at 05:13 PM
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
Your answer
Follow this Question
Related Questions
Error The namespace '' already contains a definition for 'AAMode'. 1 Answer
Two assets using the same namespace..what to do? 1 Answer
The type or namespace name NetworkInformation does not exist in the namespace System.Net 1 Answer
System.Threading.Tasks 1' does not exist in the namespace 'System.Threading.Tasks' 1 Answer