• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by exploseis5 · Dec 08, 2016 at 06:47 AM · errornamespace

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();         
      }
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JedBeryll · Dec 08, 2016 at 06:58 AM 0
Share

We need to take a look at the EnemyHealth script as well. (It's not inside a namespace is it?)

avatar image exploseis5 JedBeryll · Dec 08, 2016 at 04:34 PM 0
Share

$$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);
 }

}

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityEngine.dll doesn't fix namespace error? 1 Answer

the type or namespace cannot be found 0 Answers

Unity started rejecting my scripts? 1 Answer

Multiple errors with vuforia namespace names while building with Unity Cloud Build 1 Answer

A script by that name already exists... so what? 5 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges